Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBL-1158: Set up correct URLs for OAuth authorization endpoint #1935

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,7 @@ public final class LoginToutViewController: UIViewController, MFMailComposeViewC
}

fileprivate func pushLoginViewController() {
if featureLoginWithOAuthEnabled() {
let session = ASWebAuthenticationSession(
url: OAuth.authorizationURL(),
callbackURLScheme: OAuth.redirectScheme
) { _, _ in
// TODO: MBL-1159: Get required information from the callback; call mutation to login.
}

if featureLoginWithOAuthEnabled(), let session = OAuth.createAuthorizationSession() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this into the OAuth struct, I don't think we'll need any logic to live in LoginToutViewController.

session.presentationContextProvider = self
session.start()
} else {
Expand Down
4 changes: 2 additions & 2 deletions KsApi/PKCE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public extension Data {
/// See this documentation for more details: https://www.oauth.com/oauth2-servers/mobile-and-native-apps/authorization/
public struct PKCE {
/// Creates a random alphanumeric string of the specified length
static func createCodeVerifier(byteLength length: Int) throws -> String {
public static func createCodeVerifier(byteLength length: Int) throws -> String {
do {
var buffer = Data(count: length)
try buffer.fillWithRandomSecureBytes()
Expand All @@ -72,7 +72,7 @@ public struct PKCE {
}

/// Creates a base-64 encoded SHA256 hash of the given string.
static func createCodeChallenge(fromVerifier verifier: String) throws -> String {
public static func createCodeChallenge(fromVerifier verifier: String) throws -> String {
guard let stringData = verifier.data(using: .utf8) else {
throw PKCEError.UnexpectedRuntimeError
}
Expand Down
50 changes: 45 additions & 5 deletions Library/OAuth.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
import AuthenticationServices
import Foundation
import KsApi

public struct OAuth {
public init() {}

public static let redirectScheme = "ksrauth2"
public static func authorizationURL() -> URL {
let base = AppEnvironment.current.apiService.serverConfig.webBaseUrl
// TODO: MBL-1159: This will take URL parameters, as defined in the ticket, for PKCE
return base
static let redirectScheme = "ksrauth2"

static func authorizationURL(withCodeChallenge challenge: String) -> URL? {
let serverConfig = AppEnvironment.current.apiService.serverConfig
let baseURL = serverConfig.webBaseUrl

let parameters = [
URLQueryItem(name: "redirect_uri", value: redirectScheme),
URLQueryItem(name: "response_type", value: "code"),
URLQueryItem(name: "scope", value: "email"),
URLQueryItem(name: "client_id", value: serverConfig.apiClientAuth.clientId),
URLQueryItem(name: "code_challenge_method", value: "S256"),
URLQueryItem(name: "code_challenge", value: challenge)
]

var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)
components?.path = "/oauth/authorizations/new"
components?.queryItems = parameters

return components?.url
}

public static func createAuthorizationSession() -> ASWebAuthenticationSession? {
do {
let verifier = try PKCE.createCodeVerifier(byteLength: 32)
let challenge = try PKCE.createCodeChallenge(fromVerifier: verifier)
guard let url = authorizationURL(withCodeChallenge: challenge) else {
return nil
}

let session = ASWebAuthenticationSession(
url: url,
callbackURLScheme: OAuth.redirectScheme
) { _, _ in
// TODO: MBL-1159: Exchange information in callback for credentials, then login.
}

return session

} catch {
// TODO: Is there a way we can log/monitor these errors?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottkicks Do we have a method for log generic errors in iOS? In this case, I only expect this catch to happen if something really odd occurs, i.e. we fail to create a URL from a hardcoded string, or are unable to generate a hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to our iOS sync agenda.

return nil
}
}
}