Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Implement refresh token on iOS (#1385)
Browse files Browse the repository at this point in the history
I missed this. Woops.
  • Loading branch information
chrisbanes authored Jul 12, 2023
1 parent 6562898 commit 9bb31f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
38 changes: 34 additions & 4 deletions ios-app/Tivi/Tivi/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,36 @@ class IosTraktRefreshTokenAction: TraktRefreshTokenAction {
self.traktOAuthInfo = traktOAuthInfo
}

func invoke(state _: AuthState) async throws -> AuthState? {
// TODO:
return nil
func invoke(state: AuthState) async throws -> AuthState? {
let request = OIDTokenRequest(
configuration: configuration,
grantType: OIDGrantTypeRefreshToken,
authorizationCode: nil,
redirectURL: nil,
clientID: traktOAuthInfo.clientId,
clientSecret: traktOAuthInfo.clientSecret,
scope: nil,
refreshToken: state.refreshToken,
codeVerifier: nil,
additionalParameters: nil)

return await refresh(request: request)
}

@MainActor private func refresh(request: OIDTokenRequest) async -> AuthState? {
return await withCheckedContinuation { continuation in
OIDAuthorizationService.perform(request) { response, _ in
if let response = response {
let authState = SimpleAuthState(
accessToken: response.accessToken ?? "",
refreshToken: response.refreshToken ?? ""
)
continuation.resume(returning: authState)
} else {
continuation.resume(returning: nil)
}
}
}
}
}

Expand Down Expand Up @@ -57,7 +84,10 @@ class IosTraktLoginAction: TraktLoginAction {

@MainActor private func login(request: OIDAuthorizationRequest) async -> AuthState? {
return await withCheckedContinuation { continuation in
self.appDelegate.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self.uiViewController()) { authState, _ in
self.appDelegate.currentAuthorizationFlow = OIDAuthState.authState(
byPresenting: request,
presenting: self.uiViewController()
) { authState, _ in
if let authState = authState {
let tiviAuthState = SimpleAuthState(
accessToken: authState.lastTokenResponse?.accessToken ?? "",
Expand Down
3 changes: 1 addition & 2 deletions ios-app/Tivi/Tivi/TiviApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
options _: [UIApplication.OpenURLOptionsKey: Any] = [:]
) -> Bool {
if let authorizationFlow = currentAuthorizationFlow,
authorizationFlow.resumeExternalUserAgentFlow(with: url)
{
authorizationFlow.resumeExternalUserAgentFlow(with: url) {
currentAuthorizationFlow = nil
return true
}
Expand Down

0 comments on commit 9bb31f9

Please sign in to comment.