|
| 1 | +/*! @file GTMTVAuthorizationRequest.m |
| 2 | + @brief GTMAppAuth SDK |
| 3 | + @copyright |
| 4 | + Copyright 2016 Google Inc. |
| 5 | + @copydetails |
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#import "GTMTVAuthorizationRequest.h" |
| 20 | + |
| 21 | +#ifndef GTMAPPAUTH_USER_IMPORTS |
| 22 | +#import <AppAuth/AppAuthCore.h> |
| 23 | +#import <AppAuth/OIDURLQueryComponent.h> |
| 24 | +#else // GTMAPPAUTH_USER_IMPORTS |
| 25 | +#import "AppAuthCore.h" |
| 26 | +#import "OIDURLQueryComponent.h" |
| 27 | +#endif // GTMAPPAUTH_USER_IMPORTS |
| 28 | +#import "GTMTVServiceConfiguration.h" |
| 29 | + |
| 30 | +@implementation GTMTVAuthorizationRequest |
| 31 | + |
| 32 | +- (instancetype) |
| 33 | + initWithConfiguration:(GTMTVServiceConfiguration *)configuration |
| 34 | + clientId:(NSString *)clientID |
| 35 | + clientSecret:(nullable NSString *)clientSecret |
| 36 | + scope:(nullable NSString *)scope |
| 37 | + redirectURL:(NSURL *)redirectURL |
| 38 | + responseType:(NSString *)responseType |
| 39 | + state:(nullable NSString *)state |
| 40 | + nonce:(nullable NSString *)nonce |
| 41 | + codeVerifier:(nullable NSString *)codeVerifier |
| 42 | + codeChallenge:(nullable NSString *)codeChallenge |
| 43 | + codeChallengeMethod:(nullable NSString *)codeChallengeMethod |
| 44 | + additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters { |
| 45 | + |
| 46 | + if (![configuration isKindOfClass:[GTMTVServiceConfiguration class]]) { |
| 47 | + NSAssert([configuration isKindOfClass:[GTMTVServiceConfiguration class]], |
| 48 | + @"configuration parameter must be of type GTMTVServiceConfiguration, encountered %@", |
| 49 | + NSStringFromClass([configuration class])); |
| 50 | + return nil; |
| 51 | + } |
| 52 | + |
| 53 | + return [super initWithConfiguration:configuration |
| 54 | + clientId:clientID |
| 55 | + clientSecret:clientSecret |
| 56 | + scope:scope |
| 57 | + redirectURL:redirectURL |
| 58 | + responseType:responseType |
| 59 | + state:state |
| 60 | + nonce:nonce |
| 61 | + codeVerifier:codeVerifier |
| 62 | + codeChallenge:codeChallenge |
| 63 | + codeChallengeMethod:codeChallengeMethod |
| 64 | + additionalParameters:additionalParameters]; |
| 65 | +} |
| 66 | + |
| 67 | +- (instancetype) |
| 68 | + initWithConfiguration:(GTMTVServiceConfiguration *)configuration |
| 69 | + clientId:(NSString *)clientID |
| 70 | + clientSecret:(NSString *)clientSecret |
| 71 | + scopes:(nullable NSArray<NSString *> *)scopes |
| 72 | + additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters { |
| 73 | + return [self initWithConfiguration:configuration |
| 74 | + clientId:clientID |
| 75 | + clientSecret:clientSecret |
| 76 | + scopes:scopes |
| 77 | + redirectURL:[[NSURL alloc] init] |
| 78 | + responseType:OIDResponseTypeCode |
| 79 | + additionalParameters:additionalParameters]; |
| 80 | +} |
| 81 | + |
| 82 | +#pragma mark - NSObject overrides |
| 83 | + |
| 84 | +- (NSString *)description { |
| 85 | + return [NSString stringWithFormat:@"<%@: %p, request: %@>", |
| 86 | + NSStringFromClass([self class]), |
| 87 | + self, |
| 88 | + self.authorizationRequestURL]; |
| 89 | +} |
| 90 | + |
| 91 | +#pragma mark - |
| 92 | + |
| 93 | +- (NSURLRequest *)URLRequest { |
| 94 | + OIDURLQueryComponent *query = [[OIDURLQueryComponent alloc] init]; |
| 95 | + |
| 96 | + // Required parameters. |
| 97 | + [query addParameter:@"client_id" value:self.clientID]; |
| 98 | + |
| 99 | + if (self.additionalParameters) { |
| 100 | + // Add any additional parameters the client has specified. |
| 101 | + [query addParameters:(NSDictionary *)self.additionalParameters]; |
| 102 | + } |
| 103 | + |
| 104 | + if (self.scope) { |
| 105 | + [query addParameter:@"scope" value:(NSString *)self.scope]; |
| 106 | + } |
| 107 | + |
| 108 | + static NSString *const kHTTPPost = @"POST"; |
| 109 | + static NSString *const kHTTPContentTypeHeaderKey = @"Content-Type"; |
| 110 | + static NSString *const kHTTPContentTypeHeaderValue = |
| 111 | + @"application/x-www-form-urlencoded; charset=UTF-8"; |
| 112 | + |
| 113 | + GTMTVServiceConfiguration *tvConfiguration = (GTMTVServiceConfiguration *)self.configuration; |
| 114 | + |
| 115 | + NSMutableURLRequest *URLRequest = |
| 116 | + [[NSURLRequest requestWithURL:tvConfiguration.TVAuthorizationEndpoint] mutableCopy]; |
| 117 | + URLRequest.HTTPMethod = kHTTPPost; |
| 118 | + [URLRequest setValue:kHTTPContentTypeHeaderValue forHTTPHeaderField:kHTTPContentTypeHeaderKey]; |
| 119 | + NSString *bodyString = [query URLEncodedParameters]; |
| 120 | + NSData *body = [bodyString dataUsingEncoding:NSUTF8StringEncoding]; |
| 121 | + URLRequest.HTTPBody = body; |
| 122 | + return URLRequest; |
| 123 | +} |
| 124 | + |
| 125 | +@end |
0 commit comments