From 36babe09975de30600aef9e49a4f5e6c024c6e84 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 29 Apr 2021 14:08:45 +0530 Subject: [PATCH 1/2] Show a better looking HTML page in macOS --- Source/AppAuth/macOS/OIDRedirectHTTPHandler.m | 122 ++++++++++++++---- 1 file changed, 95 insertions(+), 27 deletions(-) diff --git a/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m b/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m index 8a3df6cc2..91e63e004 100644 --- a/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m +++ b/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m @@ -27,29 +27,84 @@ #import "OIDExternalUserAgentSession.h" #import "OIDLoopbackHTTPServer.h" -/*! @brief Page that is returned following a completed authorization. Show your own page instead by - supplying a URL in @c initWithSuccessURL that the user will be redirected to. +/*! @brief Template for page that is returned following a completed authorization or authorization error. + Show your own success page instead by supplying a URL in @c initWithSuccessURL that + the user will be redirected to. */ -static NSString *const kHTMLAuthorizationComplete = - @"Authorization complete.
Return to the app."; - -/*! @brief Error warning that the @c currentAuthorizationFlow is not set on this object (likely a +static NSString *const kHTMLPageTemplate = @"" + "" + "" + "" + " " + " %@" + " " + "" + "" + "
" + "

%@

" + "

%@

" + "
" + "" + ""; + +/*! @brief Title, heading and message for HTML success page that is shown following a completed authorization. + Show your own page instead by supplying a URL in @c initWithSuccessURL that + the user will be redirected to. + */ +static NSString *const kStringsAuthorizationComplete[] = + { + @"Authorization Successful", + @"The client authorized succesfully", + @"You can now close this tab." + }; + +/*! @brief Title, heading and message for HTML error page warning that the + @c currentAuthorizationFlow is not set on this object (likely a developer error, unless the user stumbled upon the loopback server before the authorization had started completely). - @description An object conforming to @c OIDExternalUserAgentSession is returned when the + @description An object conforming to @c OIDExternalUserAgentSession is returned when the authorization is presented with @c OIDAuthorizationService::presentAuthorizationRequest:callback:. It should be set to @c currentAuthorization when using a loopback redirect. */ -static NSString *const kHTMLErrorMissingCurrentAuthorizationFlow = - @"AppAuth Error: No currentAuthorizationFlow is set on the " - "OIDRedirectHTTPHandler. Cannot process redirect."; - -/*! @brief Error warning that the URL does not represent a valid redirect. This should be rare, may - happen if the user stumbles upon the loopback server randomly. +static NSString *const kStringsErrorMissingCurrentAuthorizationFlow[] = + { + @"Authorization Error", + @"Authorization Error", + @"AppAuth Error: No currentAuthorizationFlow is set on the " + "OIDRedirectHTTPHandler. Cannot process redirect." + }; + +/*! @brief Title, heading and message for HTML error page warning that the URL does not represent a + valid redirect. This should be rare, may happen if the user stumbles upon the loopback server randomly. */ -static NSString *const kHTMLErrorRedirectNotValid = - @"AppAuth Error: Not a valid redirect."; +static NSString *const kStringsErrorRedirectNotValid[] = + { + @"Authorization Error", + @"Authorization Error", + @"AppAuth Error: Not a valid redirect." + }; @implementation OIDRedirectHTTPHandler { HTTPServer *_httpServ; @@ -134,19 +189,32 @@ - (void)HTTPConnection:(HTTPConnection *)conn didReceiveRequest:(HTTPServerReque [self stopHTTPListener]; } - // Responds to browser request. - NSString *bodyText = kHTMLAuthorizationComplete; - NSInteger httpResponseCode = (_successURL) ? 302 : 200; - // Returns an error page if a URL other than the expected redirect is requested. - if (!handled) { - if (_currentAuthorizationFlow) { - bodyText = kHTMLErrorRedirectNotValid; - httpResponseCode = 404; - } else { - bodyText = kHTMLErrorMissingCurrentAuthorizationFlow; - httpResponseCode = 400; - } + NSString *bodyText = @""; + NSInteger httpResponseCode = 0; + + if (handled) { + bodyText = [NSString stringWithFormat:kHTMLPageTemplate, + kStringsAuthorizationComplete[0], + kStringsAuthorizationComplete[1], + kStringsAuthorizationComplete[2]]; + httpResponseCode = (_successURL) ? 302 : 200; + } else if (_currentAuthorizationFlow) { + bodyText = [NSString stringWithFormat:kHTMLPageTemplate, + kStringsErrorMissingCurrentAuthorizationFlow[0], + kStringsErrorMissingCurrentAuthorizationFlow[1], + kStringsErrorMissingCurrentAuthorizationFlow[2]]; + httpResponseCode = 404; + } else { + bodyText = [NSString stringWithFormat:kHTMLPageTemplate, + kStringsErrorRedirectNotValid[0], + kStringsErrorRedirectNotValid[1], + kStringsErrorRedirectNotValid[2]]; + httpResponseCode = 400; } + + NSAssert([bodyText length] > 0, @"bodyText is empty"); + NSAssert(httpResponseCode > 0, @"httpResponseCode is %d, should be greater than 0", httpResponseCode); + NSData *data = [bodyText dataUsingEncoding:NSUTF8StringEncoding]; CFHTTPMessageRef response = CFHTTPMessageCreateResponse(kCFAllocatorDefault, From 69304aa070c8b8b611e2407346aabec1b2c0b72a Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Fri, 30 Apr 2021 08:51:13 +0530 Subject: [PATCH 2/2] Fix warning on formatting NSInteger --- Source/AppAuth/macOS/OIDRedirectHTTPHandler.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m b/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m index 91e63e004..00d4b39ef 100644 --- a/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m +++ b/Source/AppAuth/macOS/OIDRedirectHTTPHandler.m @@ -213,7 +213,7 @@ - (void)HTTPConnection:(HTTPConnection *)conn didReceiveRequest:(HTTPServerReque } NSAssert([bodyText length] > 0, @"bodyText is empty"); - NSAssert(httpResponseCode > 0, @"httpResponseCode is %d, should be greater than 0", httpResponseCode); + NSAssert(httpResponseCode > 0, @"httpResponseCode is %ld, should be greater than 0", (long) httpResponseCode); NSData *data = [bodyText dataUsingEncoding:NSUTF8StringEncoding];