Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.notifier.setAuthorizationListener((request, response, error) => {
console.log('Authorization request complete ', request, response, error);
if (response && response.code) {
const tokenHandler = new BaseTokenRequestHandler(this.requestor);
// use the code to make the token request.
const extras: StringMap = {};
if (this.environment.client_secret) {
extras['client_secret'] = this.environment.client_secret;
}
extras['code_verifier'] = request.internal['code_verifier'];
const tokenRequest = new TokenRequest({
client_id: this.environment.client_id,
redirect_uri: this.environment.redirect_uri,
grant_type: GRANT_TYPE_AUTHORIZATION_CODE,
code: response.code,
extras: extras
});
console.log('making token request:' + JSON.stringify(tokenRequest.toStringMap()));
tokenHandler.performTokenRequest(configuration, tokenRequest)
.then((tokenResponse) => {
console.log('received token response ', tokenResponse);
this._tokenResponses.next(tokenResponse);
resolve(tokenResponse);
});
} else {
reject(error);
this.notifier.setAuthorizationListener((request, response, error) => {
console.log('Authorization request complete ', request, response, error);
if (response && response.code) {
const tokenHandler = new BaseTokenRequestHandler(this.requestor);
// use the code to make the token request.
const extras: StringMap = {};
if (this.environment.client_secret) {
extras['client_secret'] = this.environment.client_secret;
}
extras['code_verifier'] = request.internal['code_verifier'];
const tokenRequest = new TokenRequest({
client_id: this.environment.client_id,
redirect_uri: this.environment.redirect_uri,
grant_type: GRANT_TYPE_AUTHORIZATION_CODE,
code: response.code,
extras: extras
});
console.log('making token request:' + JSON.stringify(tokenRequest.toStringMap()));
constructor() {
this.notifier = new AuthorizationNotifier();
this.authStateEmitter = new AuthStateEmitter();
this.authorizationHandler = new NodeBasedHandler();
this.tokenHandler = new BaseTokenRequestHandler(requestor);
// set notifier to deliver responses
this.authorizationHandler.setAuthorizationNotifier(this.notifier);
// set a listener to listen for authorization responses
// make refresh and access token requests.
this.notifier.setAuthorizationListener((request, response, error) => {
log("Authorization request complete ", request, response, error);
if (response) {
this.makeRefreshTokenRequest(response.code)
.then(result => this.performWithFreshTokens())
.then(() => {
this.authStateEmitter.emit(AuthStateEmitter.ON_TOKEN_RESPONSE);
log("All Done.");
});
constructor() {
this.notifier = new AuthorizationNotifier();
this.authStateEmitter = new AuthStateEmitter();
this.authorizationHandler = new NodeBasedHandler();
this.tokenHandler = new BaseTokenRequestHandler(requestor);
// set notifier to deliver responses
this.authorizationHandler.setAuthorizationNotifier(this.notifier);
// set a listener to listen for authorization responses
// make refresh and access token requests.
this.notifier.setAuthorizationListener((request, response, error) => {
log("Authorization request complete ", request, response, error);
if (response) {
this.makeRefreshTokenRequest(response.code)
.then(result => this.performWithFreshTokens())
.then(() => {
this.authStateEmitter.emit(AuthStateEmitter.ON_TOKEN_RESPONSE);
log("All Done.");
});
}
});
performWithFreshTokens(): Promise {
if (!this.configuration) {
log("Unknown service configuration");
return Promise.reject("Unknown service configuration");
}
if (!this.refreshToken) {
log("Missing refreshToken.");
return Promise.resolve("Missing refreshToken.");
}
if (this.accessTokenResponse && this.accessTokenResponse.isValid()) {
// do nothing
return Promise.resolve(this.accessTokenResponse.accessToken);
}
let request = new TokenRequest(
clientId,
redirectUri,
GRANT_TYPE_REFRESH_TOKEN,
undefined,
this.refreshToken
);
return this.tokenHandler
.performTokenRequest(this.configuration, request)
.then(response => {
this.accessTokenResponse = response;
return response.accessToken;
});
}
}
_performWithInitTokenRequest(code) {
if (!this.authConfiguration) {
return Promise.resolve();
}
const extra = [];
if (this.codePair) {
extra['code_verifier'] = this.codePair.codeVrifier;
log.debug(extra);
}
// use the code to make the token request.
const request = new TokenRequest(
this.config.clientId, this.config.redirectUri,
GRANT_TYPE_AUTHORIZATION_CODE, code, undefined, extra);
return this.tokenHandler.performTokenRequest(this.authConfiguration, request)
.then(response => {
this.accessTokenResponse = response;
return new AuthAccessToken(
response.accessToken,
response.refreshToken,
response.issuedAt + response.expiresIn
);
})
.then(this.tokenStoreHander);
}
_performWithFreshTokens(accessToken) {
private makeRefreshTokenRequest(code: string): Promise {
if (!this.configuration) {
log("Unknown service configuration");
return Promise.resolve();
}
// use the code to make the token request.
let request = new TokenRequest(
clientId,
redirectUri,
GRANT_TYPE_AUTHORIZATION_CODE,
code,
undefined
);
return this.tokenHandler
.performTokenRequest(this.configuration, request)
.then(response => {
log(`Refresh Token is ${response.refreshToken}`);
this.refreshToken = response.refreshToken;
this.accessTokenResponse = response;
return response;
})
.then(() => {});
_performWithFreshTokens(accessToken) {
if (!this.config || !this.authConfiguration) {
return Promise.reject('Unknown service configuration');
}
if (!accessToken || !accessToken.refreshToken) {
return Promise.resolve('Missing refreshToken.');
}
if (accessToken && accessToken.isValid()) {
log.debug('accessToken is valid');
// do nothing
return Promise.resolve(accessToken);
}
const request = new TokenRequest(
this.config.clientId, this.config.redirectUri,
GRANT_TYPE_REFRESH_TOKEN, undefined, accessToken.refreshToken);
return this.tokenHandler.performTokenRequest(this.authConfiguration, request)
.then(response => {
this.accessTokenResponse = response;
return new AuthAccessToken(
response.accessToken,
response.refreshToken,
response.issuedAt + response.expiresIn
);
})
.then(this.tokenStoreHander);
}
logout() {
function makeRefreshTokenRequest(configuration, code) {
// use the code to make the token request.
let request = new TokenRequest(
idpConfig.clientId, idpConfig.redirectUri, GRANT_TYPE_AUTHORIZATION_CODE, code, undefined, {'client_secret': idpConfig.clientSecret});
return tokenHandler.performTokenRequest(configuration, request).then(response => {
logger.info('retrieved oauth2 refresh token', {category: 'openid-connect'});
return response;
});
}
performAuthorizationRequest() {
if (!this.config || !this.authConfiguration) {
log.error('Unknown service configuration');
return;
}
this.authState = cryptoGenerateRandom(10);
// extra prameter for OAuth PKCE code_challenge
const extra = [];
if (this.codePair) {
this.codePair.generate();
log.debug('generate code pair');
extra['code_challenge_method'] = this.codePair.codeChallengeMethod;
extra['code_challenge'] = this.codePair.codeChallenge;
log.debug(extra);
}
// create a request
const request = new AuthorizationRequest(
this.config.clientId, this.config.redirectUri, this.config.scope,
AuthorizationRequest.RESPONSE_TYPE_CODE,
this.authState, extra);
this.authorizationHandler.performAuthorizationRequest(