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

API always returns 401 #905

Closed
EorlBruder opened this issue Jul 27, 2017 · 5 comments
Closed

API always returns 401 #905

EorlBruder opened this issue Jul 27, 2017 · 5 comments
Assignees
Labels
API REST API documentation support installation and configuration issues
Milestone

Comments

@EorlBruder
Copy link

I am trying to write some code interacting with the API and whatever I do, I get "Not authorized" back.
In an attempt to troubleshoot this I used the example from the docs https://shaarli.readthedocs.io/en/master/REST-API/#complete-example and this too fails completly. I adjusted $secret to reflect my "API secret", the $apiEndpoint to be "$myshaarli/v1/api/links".
In Shaarli "Enable REST API" is set. Did I forget something?

@virtualtam virtualtam added API REST API server support installation and configuration issues documentation labels Jul 28, 2017
@virtualtam
Copy link
Member

Hi @EorlBruder !

This example looks outdated, as the JWT token is passed using the Authorization HTTP header as per #751 , see https://shaarli.github.io/api-documentation/

@virtualtam virtualtam self-assigned this Jul 28, 2017
@virtualtam virtualtam added this to the 0.9.1 milestone Jul 28, 2017
@virtualtam virtualtam removed support installation and configuration issues server labels Jul 28, 2017
@EorlBruder
Copy link
Author

EorlBruder commented Jul 28, 2017

Okay, thanks for clarifying that.
Aside from the PHP-Example I was actually using the Authorization header and it's still not working. I am suspecting my JWT-Class isn't doing what it should do here. I am using this implementation: https://java.jsonwebtoken.io/

Following kotlin Code isn't working as expected:

       val iat = Date()
        logger.debug((iat.time / 1000).toString())
        val token = Jwts.builder().setHeaderParam("typ", "JWT").setIssuedAt(Date())
                .signWith(SignatureAlgorithm.HS512, "mysecret").compact()
        logger.debug(token)

        val result = get("myshaarliurl/api/v1/links", headers = mapOf("Authorization" to "Bearer $token"))
        logger.debug(result.text)

IAT returned 1501238004
Token returned eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE1MDEyMzgwMDR9.sgvjkXZiFtk1GprWSeWGKGWvdU7fO6qf0Y9fRBftRIGCS_7hv9HPUyyZvApdaixdBwmEj7cypZd6F241gbm_6Q
(This token was generated with "mysecret" and the above IAT)
With the proper secret I still got a 401.

@virtualtam
Copy link
Member

You might be interested in this Kotlin/Java example provided by @Braincoke:

@EorlBruder
Copy link
Author

@virtualtam Thank You! That snippet helped me and now it's working.

@virtualtam
Copy link
Member

Glad I could help!

@virtualtam virtualtam added the support installation and configuration issues label Aug 5, 2017
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Aug 5, 2017
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Aug 5, 2017
Relates to shaarli#903
Relates to shaarli#905

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Lucas-C pushed a commit to Lucas-C/Shaarli that referenced this issue Aug 6, 2017
Lucas-C pushed a commit to Lucas-C/Shaarli that referenced this issue Aug 6, 2017
Relates to shaarli#903
Relates to shaarli#905

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API REST API documentation support installation and configuration issues
Projects
None yet
Development

No branches or pull requests

2 participants