GithubHelp home page GithubHelp logo

uiedbook / safetoken Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 147 KB

SafeToken is an access token and refresh token authentication library for server-side Javascript applications.

License: MIT License

Shell 1.43% TypeScript 68.00% JavaScript 30.57%
0auth2 bunjs cryptography javascript jwt jwt-authentication nodejs typescript safetoken

safetoken's Introduction

SafeToken Generator/validator

SafeToken is a simplest Auth for generating secure tokens suitable for authentication purposes.

We use it to create access tokens and refresh tokens that is verifirable and store signed data.

SafeToken is easy for everyone.

Features

  • Secure Tokens: Utilizes cryptographic signature.
  • Modern api Built using modern nodejs crypto APIs.
  • Most fastest Blazingly fast .create .verify methods.
  • Fully Typed SafeToken is written in typescript and fully typed.
  • Super light-weight 2KB~ size minified and fast token creation and verification logic.

How It Works

The SafeToken class provides methods for generating tokens. Tokens are generated using crypto to enhance security. Token expiration is managed, and new tokens can be generated based on configured time intervals.

Usage

// in auth.js
const Auth = new SafeToken({
  secret: "9494d249ad9fd041f9d052e0d0b9c9e7e45bfc3f",
});

Creating a token

let token = await Auth.create({ email: "[email protected]" });
console.log({
  token,
});

Verifying a Token

console.log({
  decodedToken: await Auth.verify(token),
});

//? only decode doesn't verify
console.log({
  decodedToken: await Auth.decode(token),
});

Custom Token Lifetimes Example

The default timeout is 3600000 miniseconds = 1 hour.

You can customize token expiration times during class instantiation. by provide timewindows according to your application's requirements.

creating custom time windows

// Example of customizing token lifetimes
const Auth2 = new SafeToken({
  secret: "9494d249ad9fd041f9d052e0d0b9c9e7e45bfc3f",
  // your custom time windows
  timeWindows: {
    // ? all provided in miliseconds
    access: 3600000 /*1 hour*/,
    refresh: 2592000000 /*1 month*/,
  },
});

Usaging custom time windows

let accessToken = await Auth2.create({ email: "[email protected]" });
let refreshToken = await Auth2.create({ email: "[email protected]" });
console.log({
  accessToken,
  refreshToken,
});
console.log({
  decodedAccessToken: await Auth2.verify(accessToken, "access"),
  decodedRefreshToken: await Auth2.verify(refreshToken, "refresh"),
});
  1. Installation:
npm i safetoken

Contributing

This library provides a simple and secure way to manage authetication tokens with built-in cryptographic encryption for added security.

If you find any issues or have suggestions for improvements, feel free to contribute by opening an issue or submitting a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Feel free to adjust the information based on your project's specific considerations and security requirements.

Pizza Area

Support me via cryptos -

  • BTC: bc1q228fnx44ha9y5lvtku70pjgaeh2jj3f867nwye
  • ETH: 0xd067560fDed3B1f3244d460d5E3011BC42C4E5d7
  • LTC: ltc1quvc04rpmsurvss6ll54fvdgyh95p5kf74wppa6
  • TRX: THag6WuG4EoiB911ce9ELgN3p7DibtS6vP

safetoken's People

Contributors

fridaycandour avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.