Advertisement
StormTruppen

Untitled

May 18th, 2024
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 2.80 KB | Gaming | 0 0
  1. const { CognitoIdentityServiceProvider } = require("@aws-sdk/client-cognito-identity-provider");
  2. const openfort = require('@openfort/openfort-node').default;
  3.  
  4. const openfortClient = new openfort("SECRET");
  5.  
  6. exports.handler = async function(event, context, callback) {
  7.     try {
  8.         // Extract the ID token from the event
  9.         const idToken = getIdTokenFromEvent(event);
  10.  
  11.         // If ID token is not found, log an error and return an error response
  12.         if (!idToken) {
  13.             console.error("ID token not found in the event.");
  14.             const response = {
  15.                 statusCode: 400,
  16.                 body: JSON.stringify({ success: false, message: 'ID token not found in the event.' })
  17.             };
  18.             return callback(null, response);
  19.         }
  20.  
  21.         // Use the ID token to decode and get user information
  22.         const userId = await getUserIdFromToken(idToken);
  23.  
  24.         // Use the userId as the thirdPartyUserId in your request
  25.         const req = {
  26.             preGenerateEmbeddedAccount: true,
  27.             thirdPartyProvider: "oidc",
  28.             thirdPartyUserId: userId,
  29.         };
  30.  
  31.         const embeddedReq = {
  32.             shieldAuthProvider: openfort.ShieldAuthProvider.Openfort,
  33.             apiKey: "SECRET",
  34.             apiSecret: "SECRET",
  35.             encryptionPart: "SECRET",
  36.         };
  37.  
  38.         // Invoke the Lambda function
  39.         const openfortSession = await openfortClient.iam.createAuthPlayer(req, embeddedReq);
  40.  
  41.         // Return the updated event back to Amazon Cognito
  42.         return callback(null, event);
  43.     } catch (error) {
  44.         // Handle any errors
  45.         console.error("Error occurred:", error);
  46.         const response = {
  47.             statusCode: 500,
  48.             body: JSON.stringify({ success: false, message: 'An error occurred while processing the request.' })
  49.         };
  50.         return callback(null, response);
  51.     }
  52. };
  53.  
  54. // Function to extract the ID token from the event
  55. function getIdTokenFromEvent(event) {
  56.     if (event && event.requestContext && event.requestContext.identity && event.requestContext.identity.cognitoAuthenticationProvider) {
  57.         return event.requestContext.identity.cognitoAuthenticationProvider.split(':').pop();
  58.     } else {
  59.         return null; // Return null if required properties are not present
  60.     }
  61. }
  62.  
  63. // Function to get the user ID from the access token
  64. async function getUserIdFromToken(token) {
  65.     try {
  66.         const cognitoIdentityServiceProvider = new CognitoIdentityServiceProvider();
  67.         const params = { AccessToken: token };
  68.         const data = await cognitoIdentityServiceProvider.getUser(params);
  69.         return data.sub;
  70.     } catch (error) {
  71.         console.error("Error decoding token:", error);
  72.         throw new Error("Failed to decode token");
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement