FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    How do I verify a token with the cloudflare-worker-jwt library

    Scheduled Pinned Locked Moved Solved Q&A
    3 Posts 1 Posters 6.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • danD
      dan
      last edited by

      I want to use a token signed by FusionAuth with an RS256 key with this library: https://github.com/tsndr/cloudflare-worker-jwt

      But it doesn't say it works with JWKS (it implies it).

      How can I do this?

      --
      FusionAuth - Identity Without Constraints
      https://fusionauth.io

      danD 1 Reply Last reply
      Reply Quote 0
      • danD
        dan @dan
        last edited by dan

        Here's a more full featured implementation:

        import jwt from '@tsndr/cloudflare-worker-jwt';
        
        import dev_jwks from './jwks/dev.json';
        
        function authenticate(handler) {
            return async function (request, response) {
                let headers = request.headers;
                if (!headers.has("Authorization")) {
                    return json_error(401, "No Auth header present");
                }
        
                let auth_header = headers.get("Authorization");
                if (auth_header.indexOf("Bearer ") !== 0) {
                    return json_error(403, "Bad auth header");
                }
        
                let token = auth_header.slice(7);
        
        		let verified = await jwt.verify(token, dev_jwks.keys[0], {algorithm: "RS256"});
        		if (!verified) {
        			return json_error(403, "Bad auth token");
        		}
        
                try {
                    token = jwt.decode(token);
                } catch (e) {
                    return json_error(403, "Unable to decode token");
                }
        
                let { header: meta, payload } = token;
        		// TODO: inspect the payload of the jwt
        
                return await handler(request, response);
            };
        }
        

        where json_error is an error handler function outside the scope of this example and the JWKS file is downloaded and put into './jwks/dev.json' and the key is known to exist in the first entry in that array.

        A more sophisticated version would examine the key id from the token header and find the corresponding public key in the the JWKS array.

        --
        FusionAuth - Identity Without Constraints
        https://fusionauth.io

        1 Reply Last reply
        Reply Quote 0
        • danD
          dan @dan
          last edited by

          You have to do a few things:

          • download the JWKS file yourself (here's info on where to find it)
          • select the key
          • specify the algorithm (the library doesn't examine the header of the token to determine the algorithm)

          So here's what it might look like:

          let verification = await jwt.verify(token, jwks.keys[0], {algorithm: "RS256"});
          

          --
          FusionAuth - Identity Without Constraints
          https://fusionauth.io

          danD 1 Reply Last reply
          Reply Quote 0
          • danD
            dan @dan
            last edited by dan

            Here's a more full featured implementation:

            import jwt from '@tsndr/cloudflare-worker-jwt';
            
            import dev_jwks from './jwks/dev.json';
            
            function authenticate(handler) {
                return async function (request, response) {
                    let headers = request.headers;
                    if (!headers.has("Authorization")) {
                        return json_error(401, "No Auth header present");
                    }
            
                    let auth_header = headers.get("Authorization");
                    if (auth_header.indexOf("Bearer ") !== 0) {
                        return json_error(403, "Bad auth header");
                    }
            
                    let token = auth_header.slice(7);
            
            		let verified = await jwt.verify(token, dev_jwks.keys[0], {algorithm: "RS256"});
            		if (!verified) {
            			return json_error(403, "Bad auth token");
            		}
            
                    try {
                        token = jwt.decode(token);
                    } catch (e) {
                        return json_error(403, "Unable to decode token");
                    }
            
                    let { header: meta, payload } = token;
            		// TODO: inspect the payload of the jwt
            
                    return await handler(request, response);
                };
            }
            

            where json_error is an error handler function outside the scope of this example and the JWKS file is downloaded and put into './jwks/dev.json' and the key is known to exist in the first entry in that array.

            A more sophisticated version would examine the key id from the token header and find the corresponding public key in the the JWKS array.

            --
            FusionAuth - Identity Without Constraints
            https://fusionauth.io

            1 Reply Last reply
            Reply Quote 0
            • danD dan has marked this topic as solved on
            • danD dan moved this topic from Q&A on
            • danD dan moved this topic from Frequently Asked Questions (FAQ) on

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post