Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lapinek/cb2a24cbf45afd696ed28730b3b50199 to your computer and use it in GitHub Desktop.
Save lapinek/cb2a24cbf45afd696ed28730b3b50199 to your computer and use it in GitHub Desktop.
// ViewController.swift
// . . .
// MARK: Authorization methods
extension ViewController {
// . . .
/**
Performs the authorization code flow using a web view.
Attempts to make a request to the authorization endpoint by utilizing a web view.
Allows the web view to handle the redirection.
*/
func authorizeWithWebView(
configuration: OIDServiceConfiguration,
clientId: String,
redirectionUri: String,
scopes: [String] = [OIDScopeOpenID, OIDScopeProfile],
completion: @escaping (OIDAuthState?, Error?) -> Void
) {
// Checking if the redirection URL can be constructed.
guard let redirectURI = URL(string: redirectionUri) else {
print("Error creating redirection URL for : \(redirectionUri)")
return
}
// Building authorization request.
let request = OIDAuthorizationRequest(
configuration: configuration,
clientId: clientId,
clientSecret: nil,
scopes: scopes,
redirectURL: redirectURI,
responseType: OIDResponseTypeCode,
additionalParameters: nil
)
// Making authorization request.
print("Initiating authorization request with scopes: \(request.scope ?? "no scope requested")")
// Using web view instead of built in AppAuth methods invoking an external user-agent.
/**
Reference to the completion handler to be called on successful authorization.
The redirection URI will be processed in the web view navigation event. The code will be exchanged for tokens using the `makeTokenRequest()` method, which will need to follow by the completion callback passed in here from the `authorizeRp()` method. Since the navigation event will be handled in a different context, we need to preserve the completion block.
*/
authorizationCompletion = completion
/**
The request object reference accessible from other methods.
AppAuth methods will be used to complete the authorization flow after redirection from the authorization endpoint and need the original request details.
*/
oidAuthorizationRequest = request
// Dismissing any existing subview.
view.viewWithTag(webViewTag)?.removeFromSuperview()
// Dismissing any existing web view controller.
webViewController = nil
// Providing the web view class with initial parameters.
webViewController = WebViewController.init(
appGroup: appGroup,
appGroupCookies: appGroupCookies,
webViewFrame: view.bounds
)
// Setting this controller as the web view navigation delegate.
webViewController.wkNavigationDelegate = self
// Loading the view with the authorization URL.
webViewController.loadWebView() {
webView in
// Tracking the view by its tag.
webView.tag = self.webViewTag
self.view.addSubview(webView)
// Loading the authorization endpoint URL obtained from the AppAuth authorization request object.
webView.load(URLRequest(url: URL(string: request.authorizationRequestURL().absoluteString)!))
}
// . . .
}
// . . .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment