@@ -7,7 +7,7 @@ import { BaseClient } from "./BaseClient";
7
7
import { AuthorizationCodeUrlRequest } from "../request/AuthorizationCodeUrlRequest" ;
8
8
import { AuthorizationCodeRequest } from "../request/AuthorizationCodeRequest" ;
9
9
import { Authority } from "../authority/Authority" ;
10
- import { ServerParamsGenerator } from "../server/ServerParamsGenerator " ;
10
+ import { RequestUtils } from "../utils/RequestUtils " ;
11
11
import { RequestValidator } from "../request/RequestValidator" ;
12
12
import { GrantType } from "../utils/Constants" ;
13
13
import { Configuration } from "../config/Configuration" ;
@@ -30,16 +30,20 @@ export class AuthorizationCodeClient extends BaseClient {
30
30
}
31
31
32
32
/**
33
- * Creates a url for logging in a user.
34
- * - scopes added by default: openid, profile and offline_access.
35
- * - performs validation of the request parameters.
33
+ * Creates the URL of the authorization request letting the user input credentials and consent to the
34
+ * application. The URL target the /authorize endpoint of the authority configured in the
35
+ * application object.
36
+ *
37
+ * Once the user inputs their credentials and consents, the authority will send a response to the redirect URI
38
+ * sent in the request and should contain an authorization code, which can then be used to acquire tokens via
39
+ * acquireToken(AuthorizationCodeRequest)
36
40
* @param request
37
41
*/
38
42
async getAuthCodeUrl ( request : AuthorizationCodeUrlRequest ) : Promise < string > {
39
43
40
44
const authority : Authority = await this . createAuthority ( request && request . authority ) ;
41
45
const queryParams : Map < string , string > = this . generateAuthCodeUrlParams ( request ) ;
42
- const queryString = ServerParamsGenerator . createQueryString ( queryParams ) ;
46
+ const queryString = RequestUtils . createQueryString ( queryParams ) ;
43
47
return authority . authorizationEndpoint + "?" + queryString ;
44
48
}
45
49
@@ -59,27 +63,25 @@ export class AuthorizationCodeClient extends BaseClient {
59
63
60
64
/**
61
65
* Executes POST request to token endpoint
62
- * @param tokenEndPoint
63
- * @param body
64
- * @param headers
66
+ * @param authority
67
+ * @param request
65
68
*/
66
- private async executeTokenRequest ( authority : Authority , requestParameters : AuthorizationCodeRequest ) : Promise < string > {
69
+ private async executeTokenRequest ( authority : Authority , request : AuthorizationCodeRequest ) : Promise < string > {
67
70
68
- const tokenParameters : Map < string , string > = this . generateAuthCodeParams ( requestParameters ) ;
69
- const headers : Map < string , string > = new Map < string , string > ( ) ;
71
+ const tokenParameters : Map < string , string > = this . generateAuthCodeParams ( request ) ;
72
+ const requestBody = RequestUtils . createQueryString ( tokenParameters ) ;
73
+ const headers : Map < string , string > = this . createDefaultTokenRequestHeaders ( ) ;
70
74
71
75
let acquiredTokenResponse ;
72
76
try {
73
- acquiredTokenResponse = this . networkClient . sendPostRequestAsync < string > (
77
+ acquiredTokenResponse = this . executePostToTokenEndpoint (
74
78
authority . tokenEndpoint ,
75
- {
76
- body : ServerParamsGenerator . createQueryString ( tokenParameters ) ,
77
- headers : this . createDefaultTokenRequestHeaders ( )
78
- }
79
- ) ;
79
+ requestBody ,
80
+ headers ) ;
81
+
80
82
return acquiredTokenResponse ;
81
83
} catch ( error ) {
82
- console . log ( error . response . data ) ;
84
+ console . log ( error . response . data ) ; // TODO use logger
83
85
return error . response . data ;
84
86
}
85
87
}
@@ -91,89 +93,92 @@ export class AuthorizationCodeClient extends BaseClient {
91
93
private generateAuthCodeParams ( request : AuthorizationCodeRequest ) : Map < string , string > {
92
94
const paramsMap : Map < string , string > = new Map < string , string > ( ) ;
93
95
94
- ServerParamsGenerator . addClientId ( paramsMap , this . config . authOptions . clientId ) ;
96
+ RequestUtils . addClientId ( paramsMap , this . config . authOptions . clientId ) ;
95
97
96
98
// validate and add scopes
97
99
const scopes = RequestValidator . validateAndGenerateScopes (
98
100
request . scopes ,
99
101
this . config . authOptions . clientId
100
102
) ;
101
- ServerParamsGenerator . addScopes ( paramsMap , scopes ) ;
103
+ RequestUtils . addScopes ( paramsMap , scopes ) ;
102
104
103
105
// validate the redirectUri (to be a non null value)
104
106
RequestValidator . validateRedirectUri ( request . redirectUri ) ;
105
- ServerParamsGenerator . addRedirectUri ( paramsMap , request . redirectUri ) ;
107
+ RequestUtils . addRedirectUri ( paramsMap , request . redirectUri ) ;
106
108
107
109
// add code: user set, not validated
108
- ServerParamsGenerator . addAuthorizationCode ( paramsMap , request . code ) ;
110
+ RequestUtils . addAuthorizationCode ( paramsMap , request . code ) ;
109
111
110
112
// add code_verifier if passed
111
113
if ( request . codeVerifier ) {
112
- ServerParamsGenerator . addCodeVerifier ( paramsMap , request . codeVerifier ) ;
114
+ RequestUtils . addCodeVerifier ( paramsMap , request . codeVerifier ) ;
113
115
}
114
116
115
- ServerParamsGenerator . addGrantType ( paramsMap , GrantType . AUTHORIZATION_CODE_GRANT ) ;
117
+ RequestUtils . addGrantType ( paramsMap , GrantType . AUTHORIZATION_CODE_GRANT ) ;
116
118
117
119
return paramsMap ;
118
120
}
119
121
120
122
/**
121
123
* This API validates the `AuthorizationCodeUrlRequest` and creates a URL
122
124
* @param request
123
- * @param config
124
125
*/
125
126
private generateAuthCodeUrlParams ( request : AuthorizationCodeUrlRequest ) : Map < string , string > {
126
127
const paramsMap = new Map < string , string > ( ) ;
127
128
128
- ServerParamsGenerator . addClientId ( paramsMap , this . config . authOptions . clientId ) ;
129
+ RequestUtils . addClientId ( paramsMap , this . config . authOptions . clientId ) ;
129
130
130
131
// validate and add scopes
131
132
const scopes = RequestValidator . validateAndGenerateScopes (
132
133
request . scopes ,
133
134
this . config . authOptions . clientId
134
135
) ;
135
- ServerParamsGenerator . addScopes ( paramsMap , scopes ) ;
136
+ RequestUtils . addScopes ( paramsMap , scopes ) ;
136
137
137
138
// validate the redirectUri (to be a non null value)
138
139
RequestValidator . validateRedirectUri ( request . redirectUri ) ;
139
- ServerParamsGenerator . addRedirectUri ( paramsMap , request . redirectUri ) ;
140
+ RequestUtils . addRedirectUri ( paramsMap , request . redirectUri ) ;
140
141
141
142
// generate the correlationId if not set by the user and add
142
143
const correlationId = request . correlationId
143
144
? request . correlationId
144
145
: this . config . cryptoInterface . createNewGuid ( ) ;
145
- ServerParamsGenerator . addCorrelationId ( paramsMap , correlationId ) ;
146
+ RequestUtils . addCorrelationId ( paramsMap , correlationId ) ;
146
147
147
148
// add response_mode = fragment (currently hardcoded, have a future option to pass 'query' if the user chooses to)
148
- ServerParamsGenerator . addResponseMode ( paramsMap ) ;
149
+ RequestUtils . addResponseMode ( paramsMap , request . responseMode ) ;
149
150
150
151
// add response_type = code
151
- ServerParamsGenerator . addResponseTypeCode ( paramsMap ) ;
152
+ RequestUtils . addResponseTypeCode ( paramsMap ) ;
152
153
153
154
if ( request . codeChallenge ) {
154
155
RequestValidator . validateCodeChallengeParams ( request . codeChallenge , request . codeChallengeMethod ) ;
155
- ServerParamsGenerator . addCodeChallengeParams ( paramsMap , request . codeChallenge , request . codeChallengeMethod ) ;
156
+ RequestUtils . addCodeChallengeParams ( paramsMap , request . codeChallenge , request . codeChallengeMethod ) ;
156
157
}
157
158
158
159
if ( request . state ) {
159
- ServerParamsGenerator . addState ( paramsMap , request . state ) ;
160
+ RequestUtils . addState ( paramsMap , request . state ) ;
160
161
}
161
162
162
163
if ( request . prompt ) {
163
164
RequestValidator . validatePrompt ( request . prompt ) ;
164
- ServerParamsGenerator . addPrompt ( paramsMap , request . prompt ) ;
165
+ RequestUtils . addPrompt ( paramsMap , request . prompt ) ;
165
166
}
166
167
167
168
if ( request . loginHint ) {
168
- ServerParamsGenerator . addLoginHint ( paramsMap , request . loginHint ) ;
169
+ RequestUtils . addLoginHint ( paramsMap , request . loginHint ) ;
169
170
}
170
171
171
172
if ( request . domainHint ) {
172
- ServerParamsGenerator . addDomainHint ( paramsMap , request . domainHint ) ;
173
+ RequestUtils . addDomainHint ( paramsMap , request . domainHint ) ;
173
174
}
174
175
175
176
if ( request . nonce ) {
176
- ServerParamsGenerator . addNonce ( paramsMap , request . nonce ) ;
177
+ RequestUtils . addNonce ( paramsMap , request . nonce ) ;
178
+ }
179
+
180
+ if ( request . claims ) {
181
+ RequestUtils . addClaims ( paramsMap , request . claims ) ;
177
182
}
178
183
179
184
return paramsMap ;
0 commit comments