Giter Site home page Giter Site logo

j0lv3r4 / next-authentication Goto Github PK

View Code? Open in Web Editor NEW
57.0 57.0 8.0 1.43 MB

Authentication & Authorization library for the Next.js framework

License: MIT License

JavaScript 36.30% TypeScript 63.70%
auth authentication authorization es6 javascript nextjs react reactjs

next-authentication's Introduction

Hi there ๐Ÿ‘‹

Follow me on Mastodon

next-authentication's People

Contributors

ctrngk avatar dependabot[bot] avatar fossabot avatar j0lv3r4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

next-authentication's Issues

Write documentation for the new API

The old API relied on getInitialProps and a HOC component. The new API works with API routes and getServerSideProps so the documentation needs updates.

Example with external api

Is it possible to create an example using an external api instead of next api routes? With external API I mean a regular nodejs api running in another port but in the same domain, it can be using passport or anything simple, I just want to see how the communication would be like.

By the way, good job with the project โค๏ธ Keep it up the good work.

Add OAuth 2.0 support

The goal is to provide helpers that minimize the boilerplate to write on Next.js apps.

Handle `httponly` cookies

next-authentication assumes that all cookies aren't httponly and this might cause conflicts.

For example, I'm working on a lambda function that only does OAuth and 302 redirects back to the web app with cookies in the headers, so that the Next.js app can grab on the first load. So the library I'm using sets the cookie setting httponly true by default and I think that's okay.

So instead of changing that security setting to false I'd like to figure out to handle this in the Frontend. It might not be possible, but I'll do some research first before adding any warning or so to the library.

Implement CSRF mitigation

To set up the implementation we use the csrf library. The same that the csurf middleware uses for Express.

How csrf works

csrf helps to mitigate CSRF by first creating two tokens:

  • CSRF secret: A Cryptographically secure CSRF token, supposedly known by the server only, i.e., <secret>
  • CSRF token: A hash of the secret, the actual token, plus a salt, i.e., <salt>;<token>

Then, csrf validates the token by doing <secret>+<salt>=<token>.

How to mitigate CSRF in Next.js using csrf

CSRF mitigation should happen when a Next.js application sends a state-changing request (POST, PUT, PATCH, and DELETE) to a protected API route.

The mitigation works by:

  • Creating a secret that only the server knows and the user will use for the entire session
  • Creating a token with the secret and a salt. The token is signed as well.
  • On every request, the Next.js application must send the token in a custom request header and a cookie
  • The server validates the tokens by:
    • Validating the signatures
    • Checking that the token of the custom header and the cookie is the same (Double-submit cookie pattern)
    • Validating the token using csrf
  • If the validation succeeds then we generate a new token.

On every request to an API route, the server validates the token with the secret saved in memory. Every time the user lands on the Next.js app a secret and a token will be generated.

References:

Improve documentation

Right now the documentation only explains the API. I'd like to expand on reasons why middlewares work like that and more examples.

Add ability to refresh and validate token

Adding expiration date (exp) in the JWT it would be useful if there's a way to check the validity of the token before each request. Same with a server check of token validation.

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.