Import TV authorization flow from GTMAppAuth · openid/AppAuth-iOS@11e11fd · GitHub
Skip to content

Commit 11e11fd

Browse files
WilliamDennisssoule
authored andcommitted
Import TV authorization flow from GTMAppAuth
- Code is from tree https://github.com/google/GTMAppAuth/tree/10d138eb05ad299fd6260741e7b92b0090db0963 - I contributed this code originally to GTMAppAuth on Dec 6, 2016 google/GTMAppAuth@2eb382f
1 parent 11f3b60 commit 11e11fd

9 files changed

Lines changed: 969 additions & 0 deletions

‎Source/AppAuthTV/GTMAppAuth.h‎

Lines changed: 21 additions & 0 deletions
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*! @file GTMTVAuthorizationRequest.h
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 <Foundation/Foundation.h>
20+
21+
#ifndef GTMAPPAUTH_USER_IMPORTS
22+
#import <AppAuth/AppAuthCore.h>
23+
#else // GTMAPPAUTH_USER_IMPORTS
24+
#import "AppAuthCore.h"
25+
#endif // GTMAPPAUTH_USER_IMPORTS
26+
27+
@class GTMTVServiceConfiguration;
28+
29+
NS_ASSUME_NONNULL_BEGIN
30+
31+
/*! @brief Represents a TV and limited input device authorization request.
32+
@see https://developers.google.com/identity/protocols/OAuth2ForDevices
33+
*/
34+
@interface GTMTVAuthorizationRequest : OIDAuthorizationRequest
35+
36+
/*! @brief Creates a TV authorization request with opinionated defaults
37+
@param configuration The service's configuration.
38+
@param clientID The client identifier.
39+
@param clientSecret The client secret.
40+
@param scopes An array of scopes to combine into a single scope string per the OAuth2 spec.
41+
@param additionalParameters The client's additional authorization parameters.
42+
*/
43+
- (instancetype)
44+
initWithConfiguration:(GTMTVServiceConfiguration *)configuration
45+
clientId:(NSString *)clientID
46+
clientSecret:(NSString *)clientSecret
47+
scopes:(nullable NSArray<NSString *> *)scopes
48+
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters;
49+
50+
/*! @brief Constructs an @c NSURLRequest representing the TV authorization request.
51+
@return An @c NSURLRequest representing the TV authorization request.
52+
*/
53+
- (NSURLRequest *)URLRequest;
54+
55+
@end
56+
57+
NS_ASSUME_NONNULL_END
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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
Lines changed: 96 additions & 0 deletions

0 commit comments

Comments
 (0)