Skip to content

Commit

Permalink
Running code with Justin Richer.
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDenniss committed Mar 27, 2017
1 parent 6ef07c7 commit 463efbd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
32 changes: 22 additions & 10 deletions Example-tvOS/Source/GTMAppAuthTVExampleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@

#import "GTMSessionFetcher.h"
#import "GTMSessionFetcherService.h"
#import "GTMTVServiceConfiguration.h"

/*! @brief The OAuth client ID.
@discussion For Google, register your client at
https://console.developers.google.com/apis/credentials?project=_
*/
static NSString *const kClientID = @"YOUR_CLIENT.apps.googleusercontent.com";
static NSString *const kClientID = @"deviceflow";

/*! @brief The OAuth client secret.
@discussion For Google, register your client at
https://console.developers.google.com/apis/credentials?project=_
*/
static NSString *const kClientSecret = @"YOUR_CLIENT_SECRET";
static NSString *const kClientSecret = @"secret";

/*! @brief NSCoding key for the authorization property.
*/
Expand Down Expand Up @@ -80,8 +81,19 @@ - (IBAction)signin:(id)sender {
[self cancelSignIn:nil];
}


NSURL *deviceAuthorizationEndpoint = [NSURL URLWithString:@"https://mitreid.org/devicecode"];
NSURL *tokenEndpoint = [NSURL URLWithString:@"https://mitreid.org/token"];


// builds authentication request
GTMTVServiceConfiguration *configuration = [GTMTVAuthorizationService TVConfigurationForGoogle];
GTMTVServiceConfiguration *configuration =
[[GTMTVServiceConfiguration alloc] initWithAuthorizationEndpoint:deviceAuthorizationEndpoint
TVAuthorizationEndpoint:deviceAuthorizationEndpoint
tokenEndpoint:tokenEndpoint];



GTMTVAuthorizationRequest *request =
[[GTMTVAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kClientID
Expand Down Expand Up @@ -143,12 +155,12 @@ - (void)saveState {
/*! @brief Loads the @c GTMAppAuthFetcherAuthorization from @c NSUSerDefaults.
*/
- (void)loadState {
GTMAppAuthFetcherAuthorization* authorization =
[GTMAppAuthFetcherAuthorization authorizationFromKeychainForName:kExampleAuthorizerKey];
[self setAuthorization:authorization];
if (authorization) {
[self logMessage:@"Authorization restored: %@", authorization];
}
// GTMAppAuthFetcherAuthorization* authorization =
// [GTMAppAuthFetcherAuthorization authorizationFromKeychainForName:kExampleAuthorizerKey];
// [self setAuthorization:authorization];
// if (authorization) {
// [self logMessage:@"Authorization restored: %@", authorization];
// }
}

/*! @brief Refreshes UI, typically called after the auth state changed.
Expand Down Expand Up @@ -176,7 +188,7 @@ - (IBAction)userinfo:(nullable id)sender {
fetcherService.authorizer = self.authorization;

// Creates a fetcher for the API call.
NSURL *userinfoEndpoint = [NSURL URLWithString:@"https://www.googleapis.com/oauth2/v3/userinfo"];
NSURL *userinfoEndpoint = [NSURL URLWithString:@"https://mitreid.org/userinfo"];
GTMSessionFetcher *fetcher = [fetcherService fetcherWithURL:userinfoEndpoint];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {

Expand Down
6 changes: 3 additions & 3 deletions Source/GTMTVAuthorizationResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#import "OIDFieldMapping.h"
#import "OIDTokenRequest.h"

NSString *const GTMTVDeviceTokenGrantType = @"http://oauth.net/grant_type/device/1.0";
NSString *const GTMTVDeviceTokenGrantType = @"urn:ietf:params:oauth:grant-type:device_code";

This comment has been minimized.

Copy link
@WilliamDenniss

WilliamDenniss Jun 2, 2020

Author Owner

This is the new RFC defined grant type


/*! @brief The key for the @c verificationURL property in the incoming parameters and for
@c NSSecureCoding.
*/
static NSString *const kVerificationURLKey = @"verification_url";
static NSString *const kVerificationURLKey = @"verification_uri";

This comment has been minimized.

Copy link
@WilliamDenniss

WilliamDenniss Jun 2, 2020

Author Owner

This parameter was renamed.


/*! @brief The key for the @c userCode property in the incoming parameters and for
@c NSSecureCoding.
Expand Down Expand Up @@ -154,7 +154,7 @@ - (OIDTokenRequest *)tokenPollRequestWithAdditionalParameters:
scopes:nil
refreshToken:nil
codeVerifier:nil
additionalParameters:nil];
additionalParameters:@{@"device_code": _deviceCode}];

This comment has been minimized.

Copy link
@WilliamDenniss

WilliamDenniss Jun 2, 2020

Author Owner

The token request is expecting the parameter code, but for the device flow the parameter is named device_code.

return pollRequest;
}

Expand Down
6 changes: 6 additions & 0 deletions Source/GTMTVAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ + (GTMTVAuthorizationCancelBlock)authorizeTVRequest:(GTMTVAuthorizationRequest *
// Starting polling interval (may be increased if a slow down message is received).
__block NSTimeInterval interval = [TVAuthorizationResponse.interval doubleValue];

interval = MAX(interval, 5);

// TODO: default interval

This comment has been minimized.

Copy link
@WilliamDenniss

WilliamDenniss Jun 2, 2020

Author Owner

The RFC specifies that the default should be 5 when no value is set.

   interval
      OPTIONAL.  The minimum amount of time in seconds that the client
      SHOULD wait between polling requests to the token endpoint.  If no
      value is provided, clients MUST use 5 as the default.

We should take two actions:

  1. Set the value to 5 when none is given by the server
  2. Have our own minimum value of at least 1s to avoid creating problems for a server that were to respond with "0" (if the interval is "0", this code will flood the server with requests - something that I have seen personally)
// Polls the token endpoint until the authorization completes or expires.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
do {
Expand All @@ -196,6 +200,8 @@ + (GTMTVAuthorizationCancelBlock)authorizeTVRequest:(GTMTVAuthorizationRequest *
break;
}

NSLog(@"Poll request: %@", pollRequest);

// Polls token endpoint.
[OIDAuthorizationService performTokenRequest:pollRequest
callback:^(OIDTokenResponse *_Nullable tokenResponse,
Expand Down

0 comments on commit 463efbd

Please sign in to comment.