Skip to content

Commit ac7d4b5

Browse files
authored
Merge pull request #2 from tikurahul/master
Updgrade to the latest versions of AppAuth-JS.
2 parents 41f6142 + 0921d1a commit ac7d4b5

File tree

4 files changed

+1520
-69
lines changed

4 files changed

+1520
-69
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v7.4.0
1+
v8.11.1

flow.ts

Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,50 @@
1414
* the License.
1515
*/
1616

17-
import {AuthorizationRequest} from '@openid/appauth/built/authorization_request';
18-
import {AuthorizationNotifier, AuthorizationRequestHandler, AuthorizationRequestResponse, BUILT_IN_PARAMETERS} from '@openid/appauth/built/authorization_request_handler';
19-
import {AuthorizationResponse} from '@openid/appauth/built/authorization_response';
20-
import {AuthorizationServiceConfiguration} from '@openid/appauth/built/authorization_service_configuration';
21-
import {NodeBasedHandler} from '@openid/appauth/built/node_support/node_request_handler';
22-
import {NodeRequestor} from '@openid/appauth/built/node_support/node_requestor';
23-
import {GRANT_TYPE_AUTHORIZATION_CODE, GRANT_TYPE_REFRESH_TOKEN, TokenRequest} from '@openid/appauth/built/token_request';
24-
import {BaseTokenRequestHandler, TokenRequestHandler} from '@openid/appauth/built/token_request_handler';
25-
import {TokenError, TokenResponse} from '@openid/appauth/built/token_response';
26-
import EventEmitter = require('events');
27-
28-
import {log} from './logger';
29-
import {StringMap} from '@openid/appauth/built/types';
17+
import { AuthorizationRequest } from "@openid/appauth/built/authorization_request";
18+
import {
19+
AuthorizationNotifier,
20+
AuthorizationRequestHandler,
21+
AuthorizationRequestResponse,
22+
BUILT_IN_PARAMETERS
23+
} from "@openid/appauth/built/authorization_request_handler";
24+
import { AuthorizationResponse } from "@openid/appauth/built/authorization_response";
25+
import { AuthorizationServiceConfiguration } from "@openid/appauth/built/authorization_service_configuration";
26+
import { NodeBasedHandler } from "@openid/appauth/built/node_support/node_request_handler";
27+
import { NodeRequestor } from "@openid/appauth/built/node_support/node_requestor";
28+
import {
29+
GRANT_TYPE_AUTHORIZATION_CODE,
30+
GRANT_TYPE_REFRESH_TOKEN,
31+
TokenRequest
32+
} from "@openid/appauth/built/token_request";
33+
import {
34+
BaseTokenRequestHandler,
35+
TokenRequestHandler
36+
} from "@openid/appauth/built/token_request_handler";
37+
import {
38+
TokenError,
39+
TokenResponse
40+
} from "@openid/appauth/built/token_response";
41+
import EventEmitter = require("events");
42+
43+
import { log } from "./logger";
44+
import { StringMap } from "@openid/appauth/built/types";
3045

3146
export class AuthStateEmitter extends EventEmitter {
32-
static ON_TOKEN_RESPONSE = 'on_token_response';
47+
static ON_TOKEN_RESPONSE = "on_token_response";
3348
}
3449

3550
/* the Node.js based HTTP client. */
3651
const requestor = new NodeRequestor();
3752

3853
/* an example open id connect provider */
39-
const openIdConnectUrl = 'https://accounts.google.com';
54+
const openIdConnectUrl = "https://accounts.google.com";
4055

4156
/* example client configuration */
4257
const clientId =
43-
'511828570984-7nmej36h9j2tebiqmpqh835naet4vci4.apps.googleusercontent.com';
44-
const redirectUri = 'http://127.0.0.1:8000';
45-
const scope = 'openid';
58+
"511828570984-7nmej36h9j2tebiqmpqh835naet4vci4.apps.googleusercontent.com";
59+
const redirectUri = "http://127.0.0.1:8000";
60+
const scope = "openid";
4661

4762
export class AuthFlow {
4863
private notifier: AuthorizationNotifier;
@@ -51,10 +66,10 @@ export class AuthFlow {
5166
readonly authStateEmitter: AuthStateEmitter;
5267

5368
// state
54-
private configuration: AuthorizationServiceConfiguration|null;
69+
private configuration: AuthorizationServiceConfiguration | undefined;
5570

56-
private refreshToken: string|undefined;
57-
private accessTokenResponse: TokenResponse|null;
71+
private refreshToken: string | undefined;
72+
private accessTokenResponse: TokenResponse | undefined;
5873

5974
constructor() {
6075
this.notifier = new AuthorizationNotifier();
@@ -66,66 +81,80 @@ export class AuthFlow {
6681
// set a listener to listen for authorization responses
6782
// make refresh and access token requests.
6883
this.notifier.setAuthorizationListener((request, response, error) => {
69-
log('Authorization request complete ', request, response, error);
84+
log("Authorization request complete ", request, response, error);
7085
if (response) {
7186
this.makeRefreshTokenRequest(response.code)
72-
.then(result => this.performWithFreshTokens())
73-
.then(() => {
74-
this.authStateEmitter.emit(AuthStateEmitter.ON_TOKEN_RESPONSE);
75-
log('All Done.');
76-
})
87+
.then(result => this.performWithFreshTokens())
88+
.then(() => {
89+
this.authStateEmitter.emit(AuthStateEmitter.ON_TOKEN_RESPONSE);
90+
log("All Done.");
91+
});
7792
}
7893
});
7994
}
8095

8196
fetchServiceConfiguration(): Promise<void> {
82-
return AuthorizationServiceConfiguration
83-
.fetchFromIssuer(openIdConnectUrl, requestor)
84-
.then(response => {
85-
log('Fetched service configuration', response);
86-
this.configuration = response;
87-
});
97+
return AuthorizationServiceConfiguration.fetchFromIssuer(
98+
openIdConnectUrl,
99+
requestor
100+
).then(response => {
101+
log("Fetched service configuration", response);
102+
this.configuration = response;
103+
});
88104
}
89105

90106
makeAuthorizationRequest(username?: string) {
91107
if (!this.configuration) {
92-
log('Unknown service configuration');
108+
log("Unknown service configuration");
93109
return;
94110
}
95111

96-
const extras: StringMap = {'prompt': 'consent', 'access_type': 'offline'};
112+
const extras: StringMap = { prompt: "consent", access_type: "offline" };
97113
if (username) {
98-
extras['login_hint'] = username;
114+
extras["login_hint"] = username;
99115
}
100116

101117
// create a request
102118
const request = new AuthorizationRequest(
103-
clientId, redirectUri, scope, AuthorizationRequest.RESPONSE_TYPE_CODE,
104-
undefined /* state */, extras);
119+
clientId,
120+
redirectUri,
121+
scope,
122+
AuthorizationRequest.RESPONSE_TYPE_CODE,
123+
undefined /* state */,
124+
extras
125+
);
105126

106-
log('Making authorization request ', this.configuration, request);
127+
log("Making authorization request ", this.configuration, request);
107128

108129
this.authorizationHandler.performAuthorizationRequest(
109-
this.configuration, request);
130+
this.configuration,
131+
request
132+
);
110133
}
111134

112135
private makeRefreshTokenRequest(code: string): Promise<void> {
113136
if (!this.configuration) {
114-
log('Unknown service configuration');
137+
log("Unknown service configuration");
115138
return Promise.resolve();
116139
}
117140
// use the code to make the token request.
118141
let request = new TokenRequest(
119-
clientId, redirectUri, GRANT_TYPE_AUTHORIZATION_CODE, code, undefined);
120-
121-
return this.tokenHandler.performTokenRequest(this.configuration, request)
122-
.then(response => {
123-
log(`Refresh Token is ${response.refreshToken}`);
124-
this.refreshToken = response.refreshToken;
125-
this.accessTokenResponse = response;
126-
return response;
127-
})
128-
.then(() => {});
142+
clientId,
143+
redirectUri,
144+
GRANT_TYPE_AUTHORIZATION_CODE,
145+
code,
146+
undefined
147+
);
148+
149+
return this.tokenHandler
150+
.performTokenRequest(this.configuration, request)
151+
.then(response => {
152+
log(`Refresh Token is ${response.refreshToken}`);
153+
this.refreshToken = response.refreshToken;
154+
this.accessTokenResponse = response;
155+
return response;
156+
})
157+
.then(() => {});
129158
}
130159

131160
loggedIn(): boolean {
@@ -134,29 +163,34 @@ export class AuthFlow {
134163

135164
signOut() {
136165
// forget all cached token state
137-
this.accessTokenResponse = null;
166+
this.accessTokenResponse = undefined;
138167
}
139168

140169
performWithFreshTokens(): Promise<string> {
141170
if (!this.configuration) {
142-
log('Unknown service configuration');
143-
return Promise.reject('Unknown service configuration');
171+
log("Unknown service configuration");
172+
return Promise.reject("Unknown service configuration");
144173
}
145174
if (!this.refreshToken) {
146-
log('Missing refreshToken.');
147-
return Promise.resolve('Missing refreshToken.');
175+
log("Missing refreshToken.");
176+
return Promise.resolve("Missing refreshToken.");
148177
}
149178
if (this.accessTokenResponse && this.accessTokenResponse.isValid()) {
150179
// do nothing
151180
return Promise.resolve(this.accessTokenResponse.accessToken);
152181
}
153182
let request = new TokenRequest(
154-
clientId, redirectUri, GRANT_TYPE_REFRESH_TOKEN, undefined,
155-
this.refreshToken);
156-
return this.tokenHandler.performTokenRequest(this.configuration, request)
157-
.then(response => {
158-
this.accessTokenResponse = response;
159-
return response.accessToken;
160-
});
183+
clientId,
184+
redirectUri,
185+
GRANT_TYPE_REFRESH_TOKEN,
186+
undefined,
187+
this.refreshToken
188+
);
189+
return this.tokenHandler
190+
.performTokenRequest(this.configuration, request)
191+
.then(response => {
192+
this.accessTokenResponse = response;
193+
return response.accessToken;
194+
});
161195
}
162196
}

0 commit comments

Comments
 (0)