forked from openid/AppAuth-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOIDAuthorizationRequest.m
300 lines (254 loc) · 11.6 KB
/
OIDAuthorizationRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*! @file OIDAuthorizationRequest.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "OIDAuthorizationRequest.h"
#import "OIDDefines.h"
#import "OIDScopeUtilities.h"
#import "OIDServiceConfiguration.h"
#import "OIDTokenUtilities.h"
#import "OIDURLQueryComponent.h"
/*! @brief The key for the @c configuration property for @c NSSecureCoding
*/
static NSString *const kConfigurationKey = @"configuration";
/*! @brief Key used to encode the @c responseType property for @c NSSecureCoding, and on the URL
request.
*/
static NSString *const kResponseTypeKey = @"response_type";
/*! @brief Key used to encode the @c clientID property for @c NSSecureCoding, and on the URL
request.
*/
static NSString *const kClientIDKey = @"client_id";
/*! @brief Key used to encode the @c clientSecret property for @c NSSecureCoding.
*/
static NSString *const kClientSecretKey = @"client_secret";
/*! @brief Key used to encode the @c scope property for @c NSSecureCoding, and on the URL request.
*/
static NSString *const kScopeKey = @"scope";
/*! @brief Key used to encode the @c redirectURL property for @c NSSecureCoding, and on the URL
request.
*/
static NSString *const kRedirectURLKey = @"redirect_uri";
/*! @brief Key used to encode the @c state property for @c NSSecureCoding, and on the URL request.
*/
static NSString *const kStateKey = @"state";
/*! @brief Key used to encode the @c codeVerifier property for @c NSSecureCoding.
*/
static NSString *const kCodeVerifierKey = @"code_verifier";
/*! @brief Key used to send the @c codeChallenge on the URL request.
*/
static NSString *const kCodeChallengeKey = @"code_challenge";
/*! @brief Key used to send the @c codeChallengeMethod on the URL request.
*/
static NSString *const kCodeChallengeMethodKey = @"code_challenge_method";
/*! @brief Key used to encode the @c additionalParameters property for
@c NSSecureCoding
*/
static NSString *const kAdditionalParametersKey = @"additionalParameters";
/*! @brief Number of random bytes generated for the @ state.
*/
static NSUInteger const kStateSizeBytes = 32;
/*! @brief Number of random bytes generated for the @ codeVerifier.
*/
static NSUInteger const kCodeVerifierBytes = 32;
NSString *const OIDOAuthorizationRequestCodeChallengeMethodS256 = @"S256";
@implementation OIDAuthorizationRequest
- (instancetype)init
OID_UNAVAILABLE_USE_INITIALIZER(
@selector(initWithConfiguration:
clientId:
scopes:
redirectURL:
responseType:
additionalParameters:)
);
- (instancetype)initWithConfiguration:(OIDServiceConfiguration *)configuration
clientId:(NSString *)clientID
clientSecret:(nullable NSString *)clientSecret
scope:(nullable NSString *)scope
redirectURL:(NSURL *)redirectURL
responseType:(NSString *)responseType
state:(nullable NSString *)state
codeVerifier:(nullable NSString *)codeVerifier
codeChallenge:(nullable NSString *)codeChallenge
codeChallengeMethod:(nullable NSString *)codeChallengeMethod
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters
{
self = [super init];
if (self) {
_configuration = [configuration copy];
_clientID = [clientID copy];
_clientSecret = [clientSecret copy];
_scope = [scope copy];
_redirectURL = [redirectURL copy];
_responseType = [responseType copy];
_state = [state copy];
_codeVerifier = [codeVerifier copy];
_codeChallenge = [codeChallenge copy];
_codeChallengeMethod = [codeChallengeMethod copy];
_additionalParameters =
[[NSDictionary alloc] initWithDictionary:additionalParameters copyItems:YES];
}
return self;
}
- (instancetype)
initWithConfiguration:(OIDServiceConfiguration *)configuration
clientId:(NSString *)clientID
clientSecret:(NSString *)clientSecret
scopes:(nullable NSArray<NSString *> *)scopes
redirectURL:(NSURL *)redirectURL
responseType:(NSString *)responseType
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters {
// generates PKCE code verifier and challenge
NSString *codeVerifier = [[self class] generateCodeVerifier];
NSString *codeChallenge = [[self class] codeChallengeS256ForVerifier:codeVerifier];
return [self initWithConfiguration:configuration
clientId:clientID
clientSecret:clientSecret
scope:[OIDScopeUtilities scopesWithArray:scopes]
redirectURL:redirectURL
responseType:responseType
state:[[self class] generateState]
codeVerifier:codeVerifier
codeChallenge:codeChallenge
codeChallengeMethod:OIDOAuthorizationRequestCodeChallengeMethodS256
additionalParameters:additionalParameters];
}
- (instancetype)
initWithConfiguration:(OIDServiceConfiguration *)configuration
clientId:(NSString *)clientID
scopes:(nullable NSArray<NSString *> *)scopes
redirectURL:(NSURL *)redirectURL
responseType:(NSString *)responseType
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters {
return [self initWithConfiguration:configuration
clientId:clientID
clientSecret:nil
scopes:scopes
redirectURL:redirectURL
responseType:responseType
additionalParameters:additionalParameters];
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(nullable NSZone *)zone {
// The documentation for NSCopying specifically advises us to return a reference to the original
// instance in the case where instances are immutable (as ours is):
// "Implement NSCopying by retaining the original instead of creating a new copy when the class
// and its contents are immutable."
return self;
}
#pragma mark - NSSecureCoding
+ (BOOL)supportsSecureCoding {
return YES;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
OIDServiceConfiguration *configuration =
[aDecoder decodeObjectOfClass:[OIDServiceConfiguration class]
forKey:kConfigurationKey];
NSString *responseType = [aDecoder decodeObjectOfClass:[NSString class] forKey:kResponseTypeKey];
NSString *clientID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kClientIDKey];
NSString *clientSecret = [aDecoder decodeObjectOfClass:[NSString class] forKey:kClientSecretKey];
NSString *scope = [aDecoder decodeObjectOfClass:[NSString class] forKey:kScopeKey];
NSURL *redirectURL = [aDecoder decodeObjectOfClass:[NSURL class] forKey:kRedirectURLKey];
NSString *state = [aDecoder decodeObjectOfClass:[NSString class] forKey:kStateKey];
NSString *codeVerifier = [aDecoder decodeObjectOfClass:[NSString class] forKey:kCodeVerifierKey];
NSString *codeChallenge =
[aDecoder decodeObjectOfClass:[NSString class] forKey:kCodeChallengeKey];
NSString *codeChallengeMethod =
[aDecoder decodeObjectOfClass:[NSString class] forKey:kCodeChallengeMethodKey];
NSSet *additionalParameterCodingClasses = [NSSet setWithArray:@[
[NSDictionary class],
[NSString class]
]];
NSDictionary *additionalParameters =
[aDecoder decodeObjectOfClasses:additionalParameterCodingClasses
forKey:kAdditionalParametersKey];
self = [self initWithConfiguration:configuration
clientId:clientID
clientSecret:clientSecret
scope:scope
redirectURL:redirectURL
responseType:responseType
state:state
codeVerifier:codeVerifier
codeChallenge:codeChallenge
codeChallengeMethod:codeChallengeMethod
additionalParameters:additionalParameters];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_configuration forKey:kConfigurationKey];
[aCoder encodeObject:_responseType forKey:kResponseTypeKey];
[aCoder encodeObject:_clientID forKey:kClientIDKey];
[aCoder encodeObject:_clientSecret forKey:kClientSecretKey];
[aCoder encodeObject:_scope forKey:kScopeKey];
[aCoder encodeObject:_redirectURL forKey:kRedirectURLKey];
[aCoder encodeObject:_state forKey:kStateKey];
[aCoder encodeObject:_codeVerifier forKey:kCodeVerifierKey];
[aCoder encodeObject:_codeChallenge forKey:kCodeChallengeKey];
[aCoder encodeObject:_codeChallengeMethod forKey:kCodeChallengeMethodKey];
[aCoder encodeObject:_additionalParameters forKey:kAdditionalParametersKey];
}
#pragma mark - NSObject overrides
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p, request: %@>",
NSStringFromClass([self class]),
self,
self.authorizationRequestURL];
}
#pragma mark - State and PKCE verifier/challenge generation Methods
+ (nullable NSString *)generateCodeVerifier {
return [OIDTokenUtilities randomURLSafeStringWithSize:kCodeVerifierBytes];
}
+ (nullable NSString *)generateState {
return [OIDTokenUtilities randomURLSafeStringWithSize:kStateSizeBytes];
}
+ (nullable NSString *)codeChallengeS256ForVerifier:(NSString *)codeVerifier {
if (!codeVerifier) {
return nil;
}
// generates the code_challenge per spec https://tools.ietf.org/html/rfc7636#section-4.2
// code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
// NB. the ASCII conversion on the code_verifier entropy was done at time of generation.
NSData *sha256Verifier = [OIDTokenUtilities sha265:codeVerifier];
return [OIDTokenUtilities encodeBase64urlNoPadding:sha256Verifier];
}
#pragma mark -
- (NSURL *)authorizationRequestURL {
OIDURLQueryComponent *query = [[OIDURLQueryComponent alloc] init];
// Required parameters.
[query addParameter:kResponseTypeKey value:_responseType];
[query addParameter:kClientIDKey value:_clientID];
// Add any additional parameters the client has specified.
[query addParameters:_additionalParameters];
// Add optional parameters, as applicable.
if (_redirectURL) {
[query addParameter:kRedirectURLKey value:_redirectURL.absoluteString];
}
if (_scope) {
[query addParameter:kScopeKey value:_scope];
}
if (_state) {
[query addParameter:kStateKey value:_state];
}
if (_codeChallenge) {
[query addParameter:kCodeChallengeKey value:_codeChallenge];
}
if (_codeChallengeMethod) {
[query addParameter:kCodeChallengeMethodKey value:_codeChallengeMethod];
}
// Construct the URL:
return [query URLByReplacingQueryInURL:_configuration.authorizationEndpoint];
}
@end