|
| 1 | +/*! @file OIDTVTokenRequest.m |
| 2 | + @brief AppAuth iOS SDK |
| 3 | + @copyright |
| 4 | + Copyright 2020 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 "OIDTVTokenRequest.h" |
| 20 | + |
| 21 | +#import "OIDDefines.h" |
| 22 | +#import "OIDTVServiceConfiguration.h" |
| 23 | +#import "OIDURLQueryComponent.h" |
| 24 | + |
| 25 | +/*! @brief The key for the @c deviceCode property for @c NSSecureCoding and request body. |
| 26 | + */ |
| 27 | +static NSString *const kDeviceCodeKey = @"device_code"; |
| 28 | + |
| 29 | +/*! @brief Key used to encode the @c grantType property for @c NSSecureCoding and request body. |
| 30 | + */ |
| 31 | +static NSString *const kGrantTypeKey = @"grant_type"; |
| 32 | + |
| 33 | +/*! @brief Value for @c grant_type key in the request body |
| 34 | + @see https://tools.ietf.org/html/rfc8628#section-3.4 |
| 35 | + */ |
| 36 | +static NSString *const kOIDTVDeviceTokenGrantType = @"urn:ietf:params:oauth:grant-type:device_code"; |
| 37 | + |
| 38 | +@implementation OIDTVTokenRequest |
| 39 | + |
| 40 | +- (instancetype)init OID_UNAVAILABLE_USE_INITIALIZER(@selector |
| 41 | + (initWithConfiguration: |
| 42 | + deviceCode: |
| 43 | + clientID: |
| 44 | + clientSecret: |
| 45 | + additionalParameters: |
| 46 | + )) |
| 47 | + |
| 48 | +- (instancetype)initWithConfiguration:(OIDServiceConfiguration *)configuration |
| 49 | + grantType:(NSString *)grantType |
| 50 | + authorizationCode:(nullable NSString *)code |
| 51 | + redirectURL:(nullable NSURL *)redirectURL |
| 52 | + clientID:(NSString *)clientID |
| 53 | + clientSecret:(nullable NSString *)clientSecret |
| 54 | + scopes:(nullable NSArray<NSString *> *)scopes |
| 55 | + refreshToken:(nullable NSString *)refreshToken |
| 56 | + codeVerifier:(nullable NSString *)codeVerifier |
| 57 | + additionalParameters: |
| 58 | + (nullable NSDictionary<NSString *, NSString *> *)additionalParameters |
| 59 | + OID_UNAVAILABLE_USE_INITIALIZER(@selector |
| 60 | + (initWithConfiguration: |
| 61 | + deviceCode: |
| 62 | + clientID: |
| 63 | + clientSecret: |
| 64 | + additionalParameters: |
| 65 | + )) |
| 66 | + |
| 67 | +- (instancetype)initWithConfiguration:(OIDServiceConfiguration *)configuration |
| 68 | + grantType:(NSString *)grantType |
| 69 | + authorizationCode:(nullable NSString *)code |
| 70 | + redirectURL:(nullable NSURL *)redirectURL |
| 71 | + clientID:(NSString *)clientID |
| 72 | + clientSecret:(nullable NSString *)clientSecret |
| 73 | + scope:(nullable NSString *)scope |
| 74 | + refreshToken:(nullable NSString *)refreshToken |
| 75 | + codeVerifier:(nullable NSString *)codeVerifier |
| 76 | + additionalParameters: |
| 77 | + (nullable NSDictionary<NSString *, NSString *> *)additionalParameters |
| 78 | + OID_UNAVAILABLE_USE_INITIALIZER(@selector |
| 79 | + (initWithConfiguration: |
| 80 | + deviceCode: |
| 81 | + clientID: |
| 82 | + clientSecret: |
| 83 | + additionalParameters: |
| 84 | + )) |
| 85 | + |
| 86 | +- (instancetype)initWithConfiguration:(OIDTVServiceConfiguration *)configuration |
| 87 | + deviceCode:(NSString *)deviceCode |
| 88 | + clientID:(NSString *)clientID |
| 89 | + clientSecret:(NSString *)clientSecret |
| 90 | + additionalParameters:(NSDictionary<NSString *, NSString *> *)additionalParameters { |
| 91 | + self = [super initWithConfiguration:configuration |
| 92 | + grantType:kOIDTVDeviceTokenGrantType |
| 93 | + authorizationCode:nil |
| 94 | + redirectURL:[[NSURL alloc] initWithString:@""] |
| 95 | + clientID:clientID |
| 96 | + clientSecret:clientSecret |
| 97 | + scope:nil |
| 98 | + refreshToken:nil |
| 99 | + codeVerifier:nil |
| 100 | + additionalParameters:additionalParameters]; |
| 101 | + |
| 102 | + if (self) { |
| 103 | + _deviceCode = [deviceCode copy]; |
| 104 | + } |
| 105 | + return self; |
| 106 | +} |
| 107 | + |
| 108 | +#pragma mark - NSCopying |
| 109 | + |
| 110 | +- (instancetype)copyWithZone:(nullable NSZone *)zone { |
| 111 | + // The documentation for NSCopying specifically advises us to return a reference to the original |
| 112 | + // instance in the case where instances are immutable (as ours is): |
| 113 | + // "Implement NSCopying by retaining the original instead of creating a new copy when the class |
| 114 | + // and its contents are immutable." |
| 115 | + return self; |
| 116 | +} |
| 117 | + |
| 118 | +#pragma mark - NSSecureCoding |
| 119 | + |
| 120 | ++ (BOOL)supportsSecureCoding { |
| 121 | + return YES; |
| 122 | +} |
| 123 | + |
| 124 | +- (instancetype)initWithCoder:(NSCoder *)aDecoder { |
| 125 | + self = [super initWithCoder:aDecoder]; |
| 126 | + if (self) { |
| 127 | + NSString *deviceCode = [aDecoder decodeObjectOfClass:[NSString class] forKey:kDeviceCodeKey]; |
| 128 | + _deviceCode = deviceCode; |
| 129 | + } |
| 130 | + return self; |
| 131 | +} |
| 132 | + |
| 133 | +- (void)encodeWithCoder:(NSCoder *)aCoder { |
| 134 | + [super encodeWithCoder:aCoder]; |
| 135 | + [aCoder encodeObject:_deviceCode forKey:kDeviceCodeKey]; |
| 136 | +} |
| 137 | + |
| 138 | +- (OIDURLQueryComponent *)tokenRequestBody { |
| 139 | + OIDURLQueryComponent *query = [[OIDURLQueryComponent alloc] init]; |
| 140 | + |
| 141 | + if (self.grantType) { |
| 142 | + [query addParameter:kGrantTypeKey value:self.grantType]; |
| 143 | + } |
| 144 | + |
| 145 | + [query addParameter:kDeviceCodeKey value:self.deviceCode]; |
| 146 | + |
| 147 | + [query addParameters:self.additionalParameters]; |
| 148 | + |
| 149 | + return query; |
| 150 | +} |
| 151 | + |
| 152 | +@end |
0 commit comments