Git Product home page Git Product logo

rocket-oauth-jwt-demo's People

Contributors

naftulikay avatar

Watchers

 avatar  avatar

rocket-oauth-jwt-demo's Issues

Issue and Verify JWT Tokens for Local Auth

In #1, we completed the login process by verifying the JWT token returned from Google.

Next, we need to issue our own JWT token and verify requests using that to complete the login flow. Ideally, we'd use asymmetric signing/verification, but for now we can start with HMAC SHA-256 with a symmetric key. Simply generate and return a JWT token from the key in the /oauth/success response, then use this token to make an authenticated request to a static API endpoint, e.g. /api/hello.

Create Auto-Refresh Service for OAuth Keystore

Presently, as of #1, we simply fetch the OAuth certificates in JSON form at the start of our service and never refresh them:

// fetch the google jwt signing certificates
let certs: JwtKeystore = isahc::get_async("https://www.googleapis.com/oauth2/v1/certs")
.await
.unwrap_or_else(|e| {
eprintln!("ERROR: Unable to fetch certificates: {}", e);
exit(1)
})
.json::<GoogleCertsResponse>()
.await
.unwrap_or_else(|e| {
eprintln!(
"ERROR: Unable to deserialize certificates from JSON response: {}",
e
);
exit(1)
})
.try_into()
.unwrap_or_else(|e| {
eprintln!("ERROR: Unable to parse certificate: {}", e);
exit(1)
});

The certificates are in a simple HashMap<String, String> format in JSON at https://www.googleapis.com/oauth2/v1/certs.

  • This resource also has an Expires header which we can use to determine when to refresh the token.
  • tokio::time::sleep_until can be used with a single task to refresh the token before the resource expires.
  • We can also set an upper bound on the refresh interval, making sure that we refresh the certificates at least once every 24 hours.
  • We can use left_right as a concurrency primitive to implement lock-free reading of certificates for the very infrequent writes that will occur.

With this implemented, we can have a HTTP service that lives forever, updating the certificates whenever Google tells us that we should with a max refresh duration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.