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

jwt for api endpoints #42

Open
ghost opened this issue Sep 8, 2020 · 12 comments
Open

jwt for api endpoints #42

ghost opened this issue Sep 8, 2020 · 12 comments

Comments

@ghost
Copy link

ghost commented Sep 8, 2020

Hi guys, @raven-chen ,

Hope you are all well !

I created the website https://paper2code.com/ with the qor framework and I need to implement a more advanced restful api functions.

So I have read the documentation about the RESTFul api but I did not find a way to implement a JWT token to query the api endpoints.

Just to be sure, and that's a very important question as I need to implement that asap, how can I implement gin-jwt with qor or as a use case with qor-example ?

Thanks for you insights or snippets by advance.

Cheers,
X

@raven-chen
Copy link
Member

hi @x0rzkov

Do you want to integrate JWT with QOR Admin resources? Or your own handler but with QOR Auth?

@ghost
Copy link

ghost commented Sep 9, 2020

@raven-chen

Both is it possible ?

But in priority, I want to create an api endpoint with a bearer and allow users to get it with their login credentials (I use auth_themes/clean); not admin users.

So if "Or your own handler but with QOR Auth?" means that so the answer is that.

Btw, it is my second handle of @x0rzkov

Cheers

@raven-chen
Copy link
Member

Both are possible. but integrate JWT into QOR Admin is not an easy job.

Since you're in rush. better to do something with the Auth.

type Auth interface {
	GetCurrentUser(*Context) qor.CurrentUser
	LoginURL(*Context) string
	LogoutURL(*Context) string
}

A quick patch would be inserting the jwt logic into GetCurrentUser. I'm not 100% sure it would work but worth a try.

@ghost
Copy link

ghost commented Sep 9, 2020

@raven-chen
Copy link
Member

I don't have a snippet. just an idea. e.g.

func (AdminAuth) GetCurrentUser(c *admin.Context) qor.CurrentUser {
        // this is a normal implementation. You can try embed JWT logic here, if user passes the jwt check, return a proper user. otherwise return nil?
	currentUser := Auth.GetCurrentUser(c.Request)
	if currentUser != nil {
		qorCurrentUser, ok := currentUser.(qor.CurrentUser)
		if !ok {
			fmt.Printf("User %#v haven't implement qor.CurrentUser interface\n", currentUser)
		}
		return qorCurrentUser
	}
	return nil
}

@sergolius
Copy link

@x0rzkov
auth module partly supports JWT authentication:

tokenString := req.Header.Get("Authorization")

You can wrap SessionStorer with decorator to extend behaviour of Update method

func (sessionStorer *SessionStorer) Update(w http.ResponseWriter, req *http.Request, claims *claims.Claims) error {

To make set "Authorization" header in response

@ghost
Copy link
Author

ghost commented Sep 9, 2020

@sergolius thanks for your reply :-)

Is there a way to create login controller without the form to get it and/or refresh it ?

Cheers,
X

@sergolius
Copy link

@x0rzkov QOR is very flexible, it's up to you to extend or override functionality.
See Password provider as base example: https://github.com/qor/auth/blob/11d4c974507d28e2fd10ff94edcdd00369e069a6/providers/password/password.go

@ghost
Copy link
Author

ghost commented Sep 9, 2020

Unfortunately, I am lost on this one, do not know how to do it... If you have a snippet, you would be my saviour ^^

@lutfuahmet
Copy link

lutfuahmet commented Oct 24, 2020

// TODO write json token

image

@lutfuahmet
Copy link

I send pull request to auth package.

#45

@ghost
Copy link
Author

ghost commented Oct 24, 2020

@lutfuahmet do you have an example with the full implementation mate ? Thanks for you reply also, greatly appreciated :-)

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

3 participants