-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOIDTokenResponse+Testing.m
More file actions
165 lines (146 loc) · 5.75 KB
/
OIDTokenResponse+Testing.m
File metadata and controls
165 lines (146 loc) · 5.75 KB
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
// Copyright 2021 Google LLC
//
// 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 "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
#import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
#import "GoogleSignIn/Tests/Unit/OIDTokenRequest+Testing.h"
#ifdef SWIFT_PACKAGE
@import AppAuth;
#else
#import <AppAuth/OIDScopeUtilities.h>
#import <AppAuth/OIDTokenRequest.h>
#import <AppAuth/OIDTokenResponse.h>
#endif
NSString *const kAccessToken = @"access_token";
NSTimeInterval const kAccessTokenExpiresIn = 3600;
NSString *const kRefreshToken = @"refresh_token";
NSString *const kServerAuthCode = @"server_auth_code";
// ID token constants
NSString *const kAlg = @"RS256";
NSString *const kKid = @"alkjdfas";
NSString *const kTyp = @"JWT";
NSString *const kUserID = @"12345679";
NSString *const kHostedDomain = @"fakehosteddomain.com";
NSString *const kIssuer = @"https://test.com";
NSString *const kAudience = @"audience";
NSString *const kAuthTime = @"1757753868";
NSTimeInterval const kIDTokenExpires = 1000;
NSTimeInterval const kIssuedAt = 0;
NSString *const kFatNameKey = @"name";
NSString *const kFatGivenNameKey = @"given_name";
NSString *const kFatFamilyNameKey = @"family_name";
NSString *const kFatPictureURLKey = @"picture";
NSString * const kFatName = @"fake username";
NSString * const kFatGivenName = @"fake";
NSString * const kFatFamilyName = @"username";
NSString * const kFatPictureURL = @"fake_user_picture_url";
@implementation OIDTokenResponse (Testing)
+ (instancetype)testInstance {
return [self testInstanceWithIDToken:[self idToken]];
}
+ (instancetype)testInstanceWithIDToken:(NSString *)idToken {
return [OIDTokenResponse testInstanceWithIDToken:idToken
accessToken:nil
expiresIn:nil
refreshToken:nil
tokenRequest:nil];
}
+ (instancetype)testInstanceWithIDToken:(NSString *)idToken
accessToken:(NSString *)accessToken
expiresIn:(NSNumber *)expiresIn
refreshToken:(NSString *)refreshToken
tokenRequest:(OIDTokenRequest *)tokenRequest {
NSMutableDictionary<NSString *, NSString *> *parameters = [[NSMutableDictionary alloc] initWithDictionary:@{
@"access_token" : accessToken ?: kAccessToken,
@"expires_in" : expiresIn ?: @(kAccessTokenExpiresIn),
@"token_type" : @"example_token_type",
@"refresh_token" : refreshToken ?: kRefreshToken,
@"scope" : [OIDScopeUtilities scopesWithArray:@[ OIDAuthorizationRequestTestingScope2 ]],
@"server_code" : kServerAuthCode,
}];
if (idToken) {
parameters[@"id_token"] = idToken;
}
return [[OIDTokenResponse alloc] initWithRequest:tokenRequest ?: [OIDTokenRequest testInstance]
parameters:parameters];
}
+ (NSString *)idToken {
return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:NO];
}
+ (NSString *)fatIDToken {
return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES];
}
+ (NSString *)fatIDTokenWithClaims {
return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES claims:YES];
}
+ (NSArray<NSString *> *)stubbedAMRValues {
return @[ @"pwd", @"mfa", @"otp" ];
}
+ (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp {
return [self idTokenWithSub:sub exp:exp fat:NO];
}
+ (NSString *)idTokenWithSub:(NSString *)sub
exp:(NSNumber *)exp
fat:(BOOL)fat {
return [self idTokenWithSub:kUserID exp:exp fat:fat claims:NO];
}
+ (NSString *)idTokenWithSub:(NSString *)sub
exp:(NSNumber *)exp
fat:(BOOL)fat
claims:(BOOL)claims {
NSError *error;
NSDictionary *headerContents = @{
@"alg" : kAlg,
@"kid" : kKid,
@"typ" : kTyp,
};
NSData *headerJson = [NSJSONSerialization dataWithJSONObject:headerContents
options:NSJSONWritingPrettyPrinted
error:&error];
if (error || !headerJson) {
return nil;
}
NSMutableDictionary<NSString *, id> *payloadContents =
[NSMutableDictionary dictionaryWithDictionary:@{
@"sub" : sub,
@"hd" : kHostedDomain,
@"iss" : kIssuer,
@"aud" : kAudience,
@"exp" : exp,
@"iat" : @(kIssuedAt),
}];
if (fat) {
[payloadContents addEntriesFromDictionary:@{
kFatNameKey : kFatName,
kFatGivenNameKey : kFatGivenName,
kFatFamilyNameKey : kFatFamilyName,
kFatPictureURLKey : kFatPictureURL,
}];
}
if (claims) {
[payloadContents addEntriesFromDictionary:@{
@"auth_time": kAuthTime,
@"amr": [OIDTokenResponse stubbedAMRValues],
}];
}
NSData *payloadJson = [NSJSONSerialization dataWithJSONObject:payloadContents
options:NSJSONWritingPrettyPrinted
error:&error];
if (error || !payloadJson) {
return nil;
}
return [NSString stringWithFormat:@"%@.%@.FakeSignature",
[headerJson base64EncodedStringWithOptions:0],
[payloadJson base64EncodedStringWithOptions:0]];
}
@end