[GH-ISSUE #484] Callback for getting valid tokens or an error #316

Closed
opened 2026-03-03 16:47:39 +03:00 by kerem · 2 comments
Owner

Originally created by @gasparuff on GitHub (Aug 20, 2018).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/484

Description:

I was using https://github.com/openid/AppAuth-iOS in my current iOS project but now I want to switch to OAuthSwift for better customization of the shown webview to log in. I have my own Networkclient implementation with RxSwift and a lot of logic, so I'd like to wrap my existing request with a function from which I receive the valid token in a callback. I don't want to build another oauthswift.client.request(...), instead I just want something like this:

oauthswift.client.getTokens { (accessToken, error) in
    // Here I would expect a valid accessToken, which I then can use in my own request. If the current accessToken is expired, I'm expecting a fresh one (refreshed with the refreshToken) or an error, in case the refreshToken is also expired already.
    let myRequest = MyRequest(url: "https://...")
    myRequest.setupToken(token: accessToken)
    myNetworkClient.execute(myRequest)
}

Is there any way to do that?

OAuth Provider? (Twitter, Github, ..):

Custom OAuth provider

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Manually

Library version:

  • head
  • v1.2.1
  • v1.2 (Swift 4.0)
  • v1.0.0
  • v0.6
  • other: (Please fill in the version you are using.)

Xcode version:

  • 9.3 (Swift 4.1)

  • 9.0 (Swift 4.0)

  • 9.0 (Swift 3.2)

  • 8.x (Swift 3.x)

  • 8.0 (Swift 2.3)

  • 7.3.1

  • other: (Please fill in the version you are using.)

  • objective c

Originally created by @gasparuff on GitHub (Aug 20, 2018). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/484 ### Description: I was using https://github.com/openid/AppAuth-iOS in my current iOS project but now I want to switch to OAuthSwift for better customization of the shown webview to log in. I have my own Networkclient implementation with RxSwift and a lot of logic, so I'd like to wrap my existing request with a function from which I receive the valid token in a callback. I don't want to build another `oauthswift.client.request(...)`, instead I just want something like this: ``` oauthswift.client.getTokens { (accessToken, error) in // Here I would expect a valid accessToken, which I then can use in my own request. If the current accessToken is expired, I'm expecting a fresh one (refreshed with the refreshToken) or an error, in case the refreshToken is also expired already. let myRequest = MyRequest(url: "https://...") myRequest.setupToken(token: accessToken) myNetworkClient.execute(myRequest) } ``` Is there any way to do that? ### OAuth Provider? (Twitter, Github, ..): Custom OAuth provider ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [x] CocoaPods - [ ] Manually ### Library version: - [ ] head - [ ] v1.2.1 - [x] v1.2 (Swift 4.0) - [ ] v1.0.0 - [ ] v0.6 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 9.3 (Swift 4.1) - [ ] 9.0 (Swift 4.0) - [ ] 9.0 (Swift 3.2) - [ ] 8.x (Swift 3.x) - [ ] 8.0 (Swift 2.3) - [ ] 7.3.1 - [ ] other: (Please fill in the version you are using.) - [ ] objective c
kerem closed this issue 2026-03-03 16:47:39 +03:00
Author
Owner

@gasparuff commented on GitHub (Aug 23, 2018):

Ok it's quite simple actually. Just wrote a small helper function for that, in case anyone needs it...

public func getTokens(completion: @escaping (_ token: String) -> Void, failCompletion: @escaping (Error?) -> Void) {
        if let credentials = self.oauthCredentials {
            if credentials.isTokenExpired() {
                debugPrint("token expired, going to refresh")
                self.oauthswift?.renewAccessToken(withRefreshToken: credentials.oauthRefreshToken, success: { (credential, response, parameters) in
                    debugPrint("token refreshed successfully")
                    self.oauthCredentials = credential
                    self.idToken = parameters["id_token"] as? String
                    completion(credential.oauthToken)
                }, failure: { (error) in
                    print("error refreshing token - probably refresh token expired: \(error)")
                    failCompletion(error)
                })
            } else {
                completion(credentials.oauthToken)
            }
        } else {
            failCompletion(nil)
        }
    }

<!-- gh-comment-id:415450104 --> @gasparuff commented on GitHub (Aug 23, 2018): Ok it's quite simple actually. Just wrote a small helper function for that, in case anyone needs it... ```swift public func getTokens(completion: @escaping (_ token: String) -> Void, failCompletion: @escaping (Error?) -> Void) { if let credentials = self.oauthCredentials { if credentials.isTokenExpired() { debugPrint("token expired, going to refresh") self.oauthswift?.renewAccessToken(withRefreshToken: credentials.oauthRefreshToken, success: { (credential, response, parameters) in debugPrint("token refreshed successfully") self.oauthCredentials = credential self.idToken = parameters["id_token"] as? String completion(credential.oauthToken) }, failure: { (error) in print("error refreshing token - probably refresh token expired: \(error)") failCompletion(error) }) } else { completion(credentials.oauthToken) } } else { failCompletion(nil) } } ```
Author
Owner

@phimage commented on GitHub (Sep 12, 2018):

Yes for oauth2 you can do that
Thanks to respond to yourself.

<!-- gh-comment-id:420538566 --> @phimage commented on GitHub (Sep 12, 2018): Yes for oauth2 you can do that Thanks to respond to yourself.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#316
No description provided.