#!/usr/bin/perl -T

#
# Perl CGI example
#

use 5.010;

use strict;
use warnings;
use CGI;
use JSON;
use MIME::Base64;

#
# This sample requires installing REST::Client from 
# cpan -- installing this will depend on your environment.
#
use REST::Client;

#
# The restApiKey value is the REST API Client Secret (the password/secret associated with your REST API Client ID). You will need
# to uncomment the @restApiKey statement and replace the empty string in the sample below with your REST API Client Secret.
# For example:  my restApiKey = "mySecretApiPassword"
# 
# The restAuthUsername value is the REST API Client ID (a GUID) automatically assigned to your account in the PingOne admin portal 
# on the Account > Integration page.
# You will need to replace the restAuthUsername value in "${restAuthUsername}" in the sample below with your REST API Client ID. 
# For example: my restAuthUsername = "5f6ce45e-1a00-488e-8519-7c9946cb6379";
#

my $q = CGI->new();
my $client = REST::Client->new();
my $token = $q->param('tokenid');
my $agent = $q->param('agentid');
my $token_endpoint = "${tokenServiceBaseUrl}/sso/TXS/2.0/1/".$token;
my restAuthUsername = "${restAuthUsername}";
my restApiKey = "";

my $auth_header = "Basic ".encode_base64(sprintf("%s:%s", $rest_username,
                                                $rest_api_key));
$client->addHeader("Authorization", $auth_header);
$client->addHeader("Cookie", "agentid=".$agent);
$client->GET($token_endpoint);
my $response = from_json($client->responseContent());
my $subject = $response->{'pingone.subject'};
my $idp_id = $response->{'pingone.idp.id'};

#
# Create a user session for $subject authenticated at $idp_id
#