|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import "GoogleSignIn/Sources/GIDAuthorizationUtil.h" |
| 18 | + |
| 19 | +#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h" |
| 20 | + |
| 21 | +#import "GoogleSignIn/Sources/GIDEMMSupport.h" |
| 22 | +#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h" |
| 23 | +#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h" |
| 24 | +#import "GoogleSignIn/Sources/GIDSignInPreferences.h" |
| 25 | + |
| 26 | +#ifdef SWIFT_PACKAGE |
| 27 | +@import AppAuth; |
| 28 | +#else |
| 29 | +#import <AppAuth/AppAuth.h> |
| 30 | +#endif |
| 31 | + |
| 32 | +NS_ASSUME_NONNULL_BEGIN |
| 33 | + |
| 34 | +@implementation GIDAuthorizationUtil |
| 35 | + |
| 36 | ++ (OIDAuthorizationRequest *) |
| 37 | + authorizationRequestWithOptions:(GIDSignInInternalOptions *)options |
| 38 | + emmSupport:(nullable NSString *)emmSupport { |
| 39 | + GIDSignInCallbackSchemes *schemes = |
| 40 | + [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID]; |
| 41 | + NSString *urlString = [NSString stringWithFormat:@"%@:%@", |
| 42 | + [schemes clientIdentifierScheme], kBrowserCallbackPath]; |
| 43 | + NSURL *redirectURL = [NSURL URLWithString:urlString]; |
| 44 | + |
| 45 | + NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy]; |
| 46 | + additionalParameters[kIncludeGrantedScopesParameter] = @"true"; |
| 47 | + if (options.configuration.serverClientID) { |
| 48 | + additionalParameters[kAudienceParameter] = options.configuration.serverClientID; |
| 49 | + } |
| 50 | + if (options.loginHint) { |
| 51 | + additionalParameters[kLoginHintParameter] = options.loginHint; |
| 52 | + } |
| 53 | + if (options.configuration.hostedDomain) { |
| 54 | + additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain; |
| 55 | + } |
| 56 | + |
| 57 | +#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
| 58 | + [additionalParameters addEntriesFromDictionary: |
| 59 | + [GIDEMMSupport parametersWithParameters:options.extraParams |
| 60 | + emmSupport:emmSupport |
| 61 | + isPasscodeInfoRequired:NO]]; |
| 62 | +#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST |
| 63 | + [additionalParameters addEntriesFromDictionary:options.extraParams]; |
| 64 | +#endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST |
| 65 | + additionalParameters[kSDKVersionLoggingParameter] = GIDVersion(); |
| 66 | + additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment(); |
| 67 | + |
| 68 | + NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL]; |
| 69 | + NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL]; |
| 70 | + OIDServiceConfiguration *appAuthConfiguration = |
| 71 | + [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL |
| 72 | + tokenEndpoint:tokenEndpointURL]; |
| 73 | + OIDAuthorizationRequest *request = |
| 74 | + [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration |
| 75 | + clientId:options.configuration.clientID |
| 76 | + scopes:options.scopes |
| 77 | + redirectURL:redirectURL |
| 78 | + responseType:OIDResponseTypeCode |
| 79 | + additionalParameters:additionalParameters]; |
| 80 | + |
| 81 | + return request; |
| 82 | +} |
| 83 | + |
| 84 | ++ (nullable NSArray<NSString *> *) |
| 85 | + resolvedScopesFromGrantedScoped:(NSArray<NSString *> *)scopes |
| 86 | + withNewScopes:(NSArray<NSString *> *)newScopes |
| 87 | + error:(NSError * __autoreleasing *)error { |
| 88 | + NSMutableSet<NSString *> *grantedScopes = [NSMutableSet setWithArray:scopes]; |
| 89 | + NSSet<NSString *> *requestedScopes = [NSSet setWithArray:newScopes]; |
| 90 | + |
| 91 | + if ([requestedScopes isSubsetOfSet:grantedScopes]) { |
| 92 | + // All requested scopes have already been granted, generate an error. |
| 93 | + *error = [NSError errorWithDomain:kGIDSignInErrorDomain |
| 94 | + code:kGIDSignInErrorCodeScopesAlreadyGranted |
| 95 | + userInfo:nil]; |
| 96 | + return nil; |
| 97 | + } |
| 98 | + |
| 99 | + // Use the union of granted and requested scopes. |
| 100 | + [grantedScopes unionSet:requestedScopes]; |
| 101 | + return [grantedScopes allObjects]; |
| 102 | +} |
| 103 | + |
| 104 | +@end |
| 105 | + |
| 106 | +NS_ASSUME_NONNULL_END |
0 commit comments