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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable access for token refresh #31

Merged
merged 15 commits into from
Jun 10, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Sources/Twift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ import Combine
public class Twift: NSObject, ObservableObject {
/// The type of authentication access for this Twift instance
public private(set) var authenticationType: AuthenticationType
public var oauthUser: OAuth2User? {
switch authenticationType {
case .oauth2UserAuth(let user):
return user
default:
return nil
}
}

/// Optional callback to be performed after a successful token refresh.
private var refreshCompletion: (()-> Void)? = nil

internal let decoder: JSONDecoder
internal let encoder: JSONEncoder

/// Initialise an instance with the specified authentication type
public init(_ authenticationType: AuthenticationType) {
public init(_ authenticationType: AuthenticationType, onRefresh: (()-> Void)? = nil) {
Copy link
Owner

Choose a reason for hiding this comment

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

Seeing this now, it actually makes more sense to have onRefresh associated with the AuthenticationType:

enum AuthenticationType {
  case oauth2User(user: OAuth2User, onRefresh: (() -> Void)?)
  ...
}

Alternatively, we might want to move to different initialisers for different authentication types. That might actually be nicer from an author perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, yeah, makes sense
Should we then even make it non-optional? I am not sure what the case would be where one would not need a refresh of the token?

I made some changes to the refreshOAuth2AccessToken and moved refreshCompletion from the init to the AuthenticationType

self.authenticationType = authenticationType

if let refreshCompletion = onRefresh {
self.refreshCompletion = refreshCompletion
}
emin-grbo marked this conversation as resolved.
Show resolved Hide resolved

self.decoder = Self.initializeDecoder()
self.encoder = Self.initializeEncoder()
Expand Down Expand Up @@ -71,6 +86,7 @@ public class Twift: NSObject, ObservableObject {
}

/// Refreshes the OAuth 2.0 token, optionally forcing a refresh even if the token is still valid
/// After a successful refresh, a user-defined callback is performed. (optional)
/// - Parameter onlyIfExpired: Set to false to force the token to refresh even if it hasn't yet expired.
public func refreshOAuth2AccessToken(onlyIfExpired: Bool = true) async throws {
guard case AuthenticationType.oauth2UserAuth(let oauthUser) = self.authenticationType else {
Expand Down Expand Up @@ -107,5 +123,9 @@ public class Twift: NSObject, ObservableObject {
refreshedOAuthUser.clientId = clientId

self.authenticationType = .oauth2UserAuth(refreshedOAuthUser)

if let userDefinedRefreshCompletion = refreshCompletion {
userDefinedRefreshCompletion()
}
}
}