11import axios from 'axios'
22import express from 'express'
3+ import jwt_decode from 'jwt-decode'
4+ import querystring from 'querystring'
35import uuid from 'uuid'
46import { axiosRequestConfig } from '../configs/request.config'
57import { CONSTANTS } from '../utils/env'
@@ -66,12 +68,15 @@ oilAuth.get('/login/callback', async (req, res) => {
6668 const redirectUrl = 'https://' + req . hostname + CONSTANTS . OIL_AUTH_CALLBACK_URL
6769 const tokenResponse = await axios ( {
6870 ...axiosRequestConfig ,
69- data : {
71+ data : querystring . stringify ( {
7072 client_id : CONSTANTS . OIL_CLIENT_ID ,
7173 client_secret : CONSTANTS . OIL_CLIENT_SECRET ,
7274 code : decodeURIComponent ( req . query . code ) ,
7375 grant_type : 'authorization_code' ,
7476 redirect_uri : redirectUrl ,
77+ } ) ,
78+ headers : {
79+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
7580 } ,
7681 method : 'POST' ,
7782 url : CONSTANTS . OIL_TOKEN_URL ,
@@ -83,17 +88,21 @@ oilAuth.get('/login/callback', async (req, res) => {
8388 } else {
8489 logError ( 'Failed to set OIL token in req session. Session not available...' )
8590 }
91+ // tslint:disable-next-line: no-any
92+ const decodedToken : any = jwt_decode ( tokenResponse . data . access_token )
93+ const userOid = decodedToken . oid
94+ logInfo ( 'User OID: ' + userOid )
8695 const userDetailResponse = await axios ( {
8796 ...axiosRequestConfig ,
8897 headers : {
89- Authorization : tokenResponse . data . access_token ,
98+ Authorization : `Bearer ${ tokenResponse . data . access_token } ` ,
9099 } ,
91100 method : 'GET' ,
92- url : CONSTANTS . OIL_USER_DETAILS_URL ,
101+ url : `https://graph.microsoft.com/v1.0/users/ ${ userOid } ` ,
93102 } )
94103
95104 logInfo ( 'User information from OIL : ' + JSON . stringify ( userDetailResponse . data ) )
96- const loginId = userDetailResponse . data . loginId
105+ const loginId = userDetailResponse . data . mail
97106 if ( ! loginId ) {
98107 const errorMessage = 'iGOT login failed. You must allow Email id on the consent form for Login. '
99108 + 'Please logout from OIL and try iGOT Login with OIL again.'
@@ -103,15 +112,15 @@ oilAuth.get('/login/callback', async (req, res) => {
103112 }
104113
105114 let result : { errMessage : string , rootOrgId : string , userExist : boolean , }
106- result = await fetchUserByEmailId ( userDetailResponse . data . loginId )
107- logInfo ( 'For OIL emailId ? ' + userDetailResponse . data . loginId + ', isUserExist ? ' + result . userExist
115+ result = await fetchUserByEmailId ( userDetailResponse . data . mail )
116+ logInfo ( 'For OIL emailId ? ' + userDetailResponse . data . mail + ', isUserExist ? ' + result . userExist
108117 + ', rootOrgId ? ' + result . rootOrgId + ', errorMessage ? ' + result . errMessage )
109118 let isFirstTimeUser = false
110119 if ( result . errMessage === '' ) {
111120 let createResult : { errMessage : string , userCreated : boolean , userId : string }
112121 if ( ! result . userExist ) {
113- logInfo ( 'iGOT User does not exist for OIL email: ' + userDetailResponse . data . loginId )
114- const mobileNo = userDetailResponse . data . MobileNo
122+ logInfo ( 'iGOT User does not exist for OIL email: ' + userDetailResponse . data . mail )
123+ const mobileNo = userDetailResponse . data . mobilePhone
115124
116125 if ( ! loginId || ! mobileNo ) {
117126 const errorMessage = 'OIL user registration failed. You must allow Email id and Mobile number on the consent form. '
@@ -120,16 +129,16 @@ oilAuth.get('/login/callback', async (req, res) => {
120129 res . redirect ( `https://${ host } /public/logout?error=` + encodeURIComponent ( errorMessage ) )
121130 return
122131 }
123- createResult = await createUserWithMailId ( userDetailResponse . data . loginId ,
124- userDetailResponse . data . FirstName , userDetailResponse . data . LastName , userDetailResponse . data . MobileNo )
132+ createResult = await createUserWithMailId ( userDetailResponse . data . mail ,
133+ userDetailResponse . data . givenName , userDetailResponse . data . surname , userDetailResponse . data . mobilePhone )
125134 if ( createResult . errMessage !== '' ) {
126135 result . errMessage = createResult . errMessage
127136 }
128137 isFirstTimeUser = true
129- logInfo ( 'New user is created for OIL email id:' + userDetailResponse . data . loginId
138+ logInfo ( 'New user is created for OIL email id:' + userDetailResponse . data . mail
130139 + ', new User id:' + createResult . userId )
131140 } else {
132- logInfo ( 'User exists for OIL email id:' + userDetailResponse . data . loginId
141+ logInfo ( 'User exists for OIL email id:' + userDetailResponse . data . mail
133142 + ', result.rootOrgId = ' + result . rootOrgId + ', XChannelId = ' + CONSTANTS . X_Channel_Id )
134143 if ( result . rootOrgId !== '' && result . rootOrgId === CONSTANTS . X_Channel_Id ) {
135144 isFirstTimeUser = true
@@ -139,21 +148,21 @@ oilAuth.get('/login/callback', async (req, res) => {
139148 let keycloakResult : {
140149 access_token : string , errMessage : string , keycloakSessionCreated : boolean , refresh_token : string
141150 }
142- keycloakResult = await updateKeycloakSession ( userDetailResponse . data . loginId , req , res )
151+ keycloakResult = await updateKeycloakSession ( userDetailResponse . data . mail , req , res )
143152 if ( keycloakResult . errMessage !== '' ) {
144- logError ( 'For OIL emailId:' + userDetailResponse . data . loginId
153+ logError ( 'For OIL emailId:' + userDetailResponse . data . mail
145154 + ', Received a keycloak error: ' + keycloakResult . errMessage )
146155 result . errMessage = keycloakResult . errMessage
147156 }
148157 logInfo ( 'OIL user session established in Keycloak: ' + JSON . stringify ( keycloakResult ) )
149158 }
150159 }
151160 if ( result . errMessage !== '' ) {
152- logError ( 'For OIL emailId:' + userDetailResponse . data . loginId
161+ logError ( 'For OIL emailId:' + userDetailResponse . data . mail
153162 + ', Received error from user search. Error Message: ' + result . errMessage )
154163 resRedirectUrl = `https://${ host } /public/logout?error=` + encodeURIComponent ( JSON . stringify ( result . errMessage ) )
155164 } else {
156- logInfo ( 'OIL login is successful for emailId:' + userDetailResponse . data . loginId )
165+ logInfo ( 'OIL login is successful for emailId:' + userDetailResponse . data . mail )
157166 if ( isFirstTimeUser ) {
158167 resRedirectUrl = `https://${ host } /public/welcome`
159168 }
0 commit comments