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

How to auth #30

Open
SerVB opened this issue Mar 31, 2020 · 10 comments
Open

How to auth #30

SerVB opened this issue Mar 31, 2020 · 10 comments

Comments

@SerVB
Copy link
Contributor

SerVB commented Mar 31, 2020

It seems that the info in #8 (comment) is irrelevant: classes and functions are missing...

I want to make my own OAuth service to be used by the main service to verify requests.

@Wicpar
Copy link
Collaborator

Wicpar commented Mar 31, 2020

Hi, i can pre-release a full OAuth server implementation i have been working on. It is not exactly maintainable enough to be production ready, but it is spec compliant. No resource server Library exists for ktor currently, only the client lib provided by ktor and the auth library provided my myndocs.

The scope of this library is only to provide a connector, the auth provider, to create the appropriate OpenAPI descriptor.

@SerVB
Copy link
Contributor Author

SerVB commented Mar 31, 2020

Oh, thank you, that will be great. Can I have a look?

@Wicpar
Copy link
Collaborator

Wicpar commented Mar 31, 2020

i got to separate the codebase from the main project first, it may take a few hours.

@Wicpar
Copy link
Collaborator

Wicpar commented Mar 31, 2020

https://github.com/papsign/KtorOAuthServer/tree/master
Example usage coming soon.

@Wicpar
Copy link
Collaborator

Wicpar commented Mar 31, 2020

https://github.com/papsign/KtorOAuthServer/tree/master/src/test/kotlin/com/papsign/oauth2/example
You need to implement the same functionality yourself, and preferably with database access.
If you use Exposed, you may need to implement the requestWrapper parameters with a database transaction
You have to register: The auth server with: Application.registerTestAuth()
And to handle the resource server call:

inline fun NormalOpenAPIRoute.oauth2(vararg scopes: OAuthScope, crossinline route: OpenAPIAuthenticatedRoute<APIPrincipal>.()->Unit = {}): OpenAPIAuthenticatedRoute<APIPrincipal> {
    return TestOpenAPIOAuthProvider(scopes.asList()).apply(this).apply {
        route()
    }
}

@Wicpar
Copy link
Collaborator

Wicpar commented Apr 1, 2020

have you got it to work ?

@SerVB
Copy link
Contributor Author

SerVB commented Apr 1, 2020

I haven't looked at it well; I'm planning to do it. However, at the first glance, it's easier for me to write my own auth server...

@Wicpar
Copy link
Collaborator

Wicpar commented Apr 1, 2020

The OAuth2 spec is a real mess, the configuration you need here is simply to set up the persistence and login validation. Most of the work is done, the only thing missing is a reusable and modular login tunnel system, which is not part of the OAuth2 spec.
If you want to avoid too much work i recommend using a simpler system like JWT authentication. OAuth2 is a lot of work. It took me two entire weeks to understand the spec and implement it in its entirety to a usable state. What you have here is about 3 hours work to provide the persistence interface and the user login strategy, even less since you already have your persistence system setup.

I can provide you with an example on how to make a proper login tunnel, but the use-case is not fully fleshed out due to intricacies with cross domain access, iframes, and multiple authentication possibilities (ie password and other oauth login providers to choose from).

The idea is to make the library evolve to make it simpler to use, it is not refined yet.

I used to use the Ktor OAuth authentication, but that is meant to allow you to get data from a service like github or google, not a resource server authorisation to provide content yourself and it slowed the service by a lot (300ms instead of 5ms).

@SerVB
Copy link
Contributor Author

SerVB commented Apr 1, 2020

Thank you, I'm not sure now that I want the same thing. I want just a simple server to generate access and refresh tokens. So maybe it won't be OAuth2-compliant...

about 3 hours work

I think I'll write my own server using this time, sorry.

@Wicpar
Copy link
Collaborator

Wicpar commented Apr 1, 2020

Alright, JWT is usually the easiest way to handle the state of the session because you don't need an underlying persistence layer, and a lot of libraries already exist. You can then require the token in the API requests and use the default ktor authentication system for that, it even is supported by swagger UI.

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