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

[Question] Further token authentication API examples #13

Closed
joshhornby opened this issue Nov 30, 2015 · 4 comments
Closed

[Question] Further token authentication API examples #13

joshhornby opened this issue Nov 30, 2015 · 4 comments

Comments

@joshhornby
Copy link

Is there any further API examples? Would be great to see how this could be implemented with a token based authentication system.

@pcantrell
Copy link
Member

There isn't a whole lot of public example code yet. You’ll find some hints in the configuration chapter of the user guide and in the API docs for invalidateConfiguration.

Although there isn’t currently time available for it, my hope to write up a cookbook of examples for the 1.0 release.

If you can pose a more specific question, I can help point you in the right direction.

@joshhornby
Copy link
Author

The cookbook sounds great.

Question wise, how would you handle oAuth 2 with Siesta?

@pcantrell
Copy link
Member

I’d first select a third-party lib such as p2/OAuth2 or dongri/OAuthSwift to handle the whole dance of acquiring an OAuth token. That's well outside the scope of Siesta.

Once you have a token, you’d do something like this:

class MyApi: Service
    {
    init()
        {
        ...

        configure { $0.config.headers["Authorization"] = authHeader }
        }

    var authHeader: String?
        {
        didSet
            {
            // Clear any cached data now that auth has changed
            wipeResources()

            // Force resources to recompute headers next time they’re fetched
            invalidateConfiguration()
            }
        }
    }

…and then when authentication succeeds:

myAPI.authToken = authTokenFromOAuthLib

…and for logout:

myAPI.authToken = nil

You could integrate OAuth more tightly with Siesta. For example, you could add special error handling hooks in your Siesta config to trigger a token refresh when you detect an OAuth error. However, I’d start with something as simple as the code above and see what other needs you have.

@pcantrell
Copy link
Member

I added some of this info to the Siesta docs: http://bustoutsolutions.github.io/siesta/guide/security/

Closing this issue, but feel free to post more on Stack Overflow (with the siesta-swift tag) or here if you need more info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants