Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Get unexpired access token #2

Closed
dungi opened this issue Nov 29, 2021 · 8 comments
Closed

Get unexpired access token #2

dungi opened this issue Nov 29, 2021 · 8 comments
Labels
question Further information is requested

Comments

@dungi
Copy link

dungi commented Nov 29, 2021

Is your feature request related to a problem? Please describe.

  • I See that the framework can create the access token automatically with the refresh token, if you do a request.

Describe the solution you'd like

  • Is there a way to get the access token string - that is not expired - manually?
@dungi dungi changed the title Refresh Token manually Get unexpired access token manually Nov 29, 2021
@Colaski
Copy link
Owner

Colaski commented Nov 29, 2021

You can get the current Access Token by initializing a Tokens instance and accessing its accessToken property (just make sure you have already called the authorizationResponseHandler method at some point). Tokens is not marked public so it's not included in the documentation (since all that stuff is handled internally by the library) but you should still be able to access Tokens when the library is imported. Tokens is documented in its source file.

You would do:

let tokens = try Tokens.init(for: clientIDString, from: keychainInstance)
let accessToken = tokens.accessToken

That initializer is documented here: https://github.com/Colaski/SwAuth/blob/main/Sources/SwAuth/Tokens.swift#L82.

I see your original title asks about refreshing the token manually, I'm curious about why you would want to do that.

@Colaski Colaski added enhancement New feature or request question Further information is requested and removed enhancement New feature or request labels Nov 29, 2021
@dungi
Copy link
Author

dungi commented Nov 29, 2021

Thanks for your answer. Thats why I didnt found that in the documentation.

I guess I ask wrong. I want to access an unexpired access token, without calling a request for it to be fresh. I using the accessToken inside a webView so I need it to be unexpired.

I will try your code later on :) thanks

@dungi dungi changed the title Get unexpired access token manually Get unexpired access token Nov 29, 2021
@Colaski
Copy link
Owner

Colaski commented Nov 29, 2021

My apologies, I miss-understood. Just to clarify, the Tokens struct contains the tokens of the most recent request to the tokens endpoint, it is only updated when an authenticated HTTP request is made and the access token is expired. When an authenticated request is made it checks whether or not the token is expired and if it is, makes a request to the token endpoint, refreshing the tokens.

I can maybe refactor the code to do as you describe.

@Colaski
Copy link
Owner

Colaski commented Nov 30, 2021

@dungi turns out no refractor required. What you should be able to do is call the checkRefreshToken method of your instance of an authorization flow and provide an instance of Tokens. Just make sure you have already called your authorization flow's authorizationResponseHandler.

Here's an example:

var tokens = try Tokens.init(for: clientIDString, from: keychainInstance)
try await someAuthFlowInstance.checkRefreshToken(tokens: &tokens)

// Your instance of Tokens has now been updated accordingly
let accessToken = tokens.accessToken

Please reach out if you have an issue or further questions! Apologies for the confusion.

@Colaski Colaski closed this as completed Nov 30, 2021
@dungi
Copy link
Author

dungi commented Dec 1, 2021

Hey,

at my first try, it doesn't seem that "Token" is reachable. Cause it's not public.
Also 'checkRefreshToken' is inaccessible due to 'internal' protection level.
Both things are needed to be accessible.

I also run into something else, but I will try to fix this myself:
I will also try to make this framework run on Xcode 13.2 (Beta) to have it available for iOS 13/14. If you are not in to it.

@Colaski
Copy link
Owner

Colaski commented Dec 1, 2021

I just pushed an update (1.0.2) that makes Tokens and checkRefreshTokens public, not sure why it was saying it's internal cus it' certainly was not marked that way (must be the default protection level).

so this should work now:

var tokens = try Tokens.init(for: clientIDString, from: keychainInstance)
try await someAuthFlowInstance.checkRefreshToken(tokens: &tokens)

let accessToken = tokens.accessToken

Once Xcode 13.2 is officially released I will push an update supporting macOS 10.15, iOS 13, tvOS 13, and watchOS 6. Should be as easy as changing some numbers around.

@dungi
Copy link
Author

dungi commented Dec 1, 2021

I guess in a framework everything that is not public, is not accessible outside of its framework. To access "tokens.accessToken" accessToken inside Token must be public too :) rest looks good for me. Thanks for your fast update.

@Colaski
Copy link
Owner

Colaski commented Dec 1, 2021

Woops, i made the methods public but not the properties, over sight on by part. Thanks for sticking with me 😅. 1.0.3 pushed just now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants