GithubHelp home page GithubHelp logo

Get unexpired access token about swauth HOT 8 CLOSED

colaski avatar colaski commented on May 29, 2024
Get unexpired access token

from swauth.

Comments (8)

Colaski avatar Colaski commented on May 29, 2024 1

@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.

from swauth.

Colaski avatar Colaski commented on May 29, 2024

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.

from swauth.

dungi avatar dungi commented on May 29, 2024

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

from swauth.

Colaski avatar Colaski commented on May 29, 2024

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.

from swauth.

dungi avatar dungi commented on May 29, 2024

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.

from swauth.

Colaski avatar Colaski commented on May 29, 2024

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.

from swauth.

dungi avatar dungi commented on May 29, 2024

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.

from swauth.

Colaski avatar Colaski commented on May 29, 2024

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.

from swauth.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.