qor/auth

jwt for api endpoints

Opened this issue · 12 comments

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

hi @x0rzkov

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

@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

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.

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
}

@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

@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

@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

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

// TODO write json token

image

I send pull request to auth package.

#45

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