Skip to content

Commit

Permalink
Incorporate OIDExternalUserAgentMac change in OIDState+Mac and modify…
Browse files Browse the repository at this point in the history
… the macOS example app.
  • Loading branch information
Alex-4-Git committed Jan 14, 2022
1 parent ede2256 commit dc062e6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 34 deletions.
104 changes: 70 additions & 34 deletions Examples/Example-macOS/Source/AppAuthExampleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,29 @@

/*! @brief The OIDC issuer from which the configuration will be discovered.
*/
static NSString *const kIssuer = @"https://issuer.example.com";
//static NSString *const kIssuer = @"https://issuer.example.com";
static NSString *const kIssuer = @"https://accounts.google.com";

/*! @brief The OAuth client ID.
@discussion For client configuration instructions, see the README.
@see https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-macOS/README.md
*/
static NSString *const kClientID = @"YOUR_CLIENT_ID";
//static NSString *const kClientID = @"YOUR_CLIENT_ID";
static NSString *const kClientID = @"1019271468193-agbre5rfma5i22814soo4g3c4kf689m7.apps.googleusercontent.com";

/*! @brief The OAuth client secret.
@discussion For client configuration instructions, see the README.
@see https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-macOS/README.md
*/
static NSString *const kClientSecret = @"YOUR_CLIENT_SECRET";
//static NSString *const kClientSecret = @"YOUR_CLIENT_SECRET";
static NSString *const kClientSecret = @"";

/*! @brief The OAuth redirect URI for the client @c kClientID.
@discussion For client configuration instructions, see the README.
@see https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-macOS/README.md
*/
static NSString *const kRedirectURI = @"com.example.app:/oauth2redirect/example-provider";
//static NSString *const kRedirectURI = @"com.example.app:/oauth2redirect/example-provider";
static NSString *const kRedirectURI = @"com.googleusercontent.apps.1019271468193-agbre5rfma5i22814soo4g3c4kf689m7:/oauth2redirect/example-provider";

/*! @brief Post-authorization redirect URI.
@discussion This is the URL that users will be redirected to after the OAuth flow is complete.
Expand Down Expand Up @@ -212,19 +216,22 @@ - (IBAction)authWithAutoCodeExchange:(nullable id)sender {
responseType:OIDResponseTypeCode
additionalParameters:nil];
// performs authentication request
self.appDelegate.currentAuthorizationFlow =
if (@available(macOS 10.15, *)) {
self.appDelegate.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
if (authState) {
[self setAuthState:authState];
[self logMessage:@"Got authorization tokens. Access token: %@",
authState.lastTokenResponse.accessToken];
} else {
[self logMessage:@"Authorization error: %@", [error localizedDescription]];
[self setAuthState:nil];
}
}];
presentingWindow:self.view.window
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
[self processAuthState:authState error:error];
}];
} else {
self.appDelegate.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
[self processAuthState:authState error:error];
}];
}
}];
}

Expand Down Expand Up @@ -280,24 +287,21 @@ - (IBAction)authWithAutoCodeExchangeHTTP:(nullable id)sender {
additionalParameters:nil];
// performs authentication request
__weak __typeof(self) weakSelf = self;
_redirectHTTPHandler.currentAuthorizationFlow =

if (@available(macOS 10.15, *)) {
_redirectHTTPHandler.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
// Brings this app to the foreground.
[[NSRunningApplication currentApplication]
activateWithOptions:(NSApplicationActivateAllWindows |
NSApplicationActivateIgnoringOtherApps)];

// Processes the authorization response.
if (authState) {
[weakSelf logMessage:@"Got authorization tokens. Access token: %@",
authState.lastTokenResponse.accessToken];
} else {
[weakSelf logMessage:@"Authorization error: %@", error.localizedDescription];
}
[weakSelf setAuthState:authState];
}];
presentingWindow:self.view.window
callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) {
[self processHTTPRedirectAuthState:authState error:error weakSelfReference:weakSelf];
}];
} else {
_redirectHTTPHandler.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) {
[self processHTTPRedirectAuthState:authState error:error weakSelfReference:weakSelf];
}];
}
}];
}

Expand Down Expand Up @@ -488,7 +492,39 @@ - (void)logMessage:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2) {
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSString *logLine = [NSString stringWithFormat:@"\n%@: %@", dateString, log];
NSAttributedString* logLineAttr = [[NSAttributedString alloc] initWithString:logLine];
[[_logTextView textStorage] appendAttributedString:logLineAttr];
dispatch_async(dispatch_get_main_queue(), ^{
[[_logTextView textStorage] appendAttributedString:logLineAttr];
});
}

- (void)processAuthState:(OIDAuthState *)authState
error:(NSError *)error {
if (authState) {
[self setAuthState:authState];
[self logMessage:@"Got authorization tokens. Access token: %@",
authState.lastTokenResponse.accessToken];
} else {
[self logMessage:@"Authorization error: %@", [error localizedDescription]];
[self setAuthState:nil];
}
}

- (void)processHTTPRedirectAuthState:(OIDAuthState *)authState
error:(NSError *)error
weakSelfReference:(AppAuthExampleViewController *)weakSelf {
// Brings this app to the foreground.
[[NSRunningApplication currentApplication]
activateWithOptions:(NSApplicationActivateAllWindows |
NSApplicationActivateIgnoringOtherApps)];

// Processes the authorization response.
if (authState) {
[weakSelf logMessage:@"Got authorization tokens. Access token: %@",
authState.lastTokenResponse.accessToken];
} else {
[weakSelf logMessage:@"Authorization error: %@", error.localizedDescription];
}
[weakSelf setAuthState:authState];
}

@end
8 changes: 8 additions & 0 deletions Source/AppAuth/macOS/OIDAuthState+Mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#if TARGET_OS_OSX

#import <AppKit/AppKit.h>
#import "OIDAuthState.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -35,11 +36,18 @@ NS_ASSUME_NONNULL_BEGIN
and update the OIDAuthState with the results (@c
OIDAuthState.updateWithTokenResponse:error:).
@param authorizationRequest The authorization request to present.
@param presentingWindow The window to present the authentication flow.
@param callback The method called when the request has completed or failed.
@return A @c OIDExternalUserAgentSession instance which will terminate when it
receives a @c OIDExternalUserAgentSession.cancel message, or after processing a
@c OIDExternalUserAgentSession.resumeExternalUserAgentFlowWithURL: message.
*/
+ (id<OIDExternalUserAgentSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
presentingWindow:(NSWindow *)presentingWindow
callback:(OIDAuthStateAuthorizationCallback)callback
API_AVAILABLE(macosx(10.15));

+ (id<OIDExternalUserAgentSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
callback:(OIDAuthStateAuthorizationCallback)callback;
Expand Down
11 changes: 11 additions & 0 deletions Source/AppAuth/macOS/OIDAuthState+Mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@

@implementation OIDAuthState (Mac)

+ (id<OIDExternalUserAgentSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
presentingWindow:(NSWindow *)presentingWindow
callback:(OIDAuthStateAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent = [[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow];

return [self authStateByPresentingAuthorizationRequest:authorizationRequest
externalUserAgent:externalUserAgent
callback:callback];
}

+ (id<OIDExternalUserAgentSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
callback:(OIDAuthStateAuthorizationCallback)callback {
Expand Down

0 comments on commit dc062e6

Please sign in to comment.