npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-oauth-server

v3.0.3

Published

OAuth provider for Fasitfy

Downloads

19

Readme

Fastify OAuth Server Build Status

Complete, compliant and well tested module for implementing an OAuth2 Server/Provider with fastify in node.js.

This is the fastify wrapper for oauth2-server.

Installation

$ npm install fastify-oauth-server

Quick Start

The module provides one decorator - oauth.

Use the decorator to get the authentication state.

// ./model.js

const argon2 = require('argon2');

const connect = require('../db');

module.exports.getAccessToken = async (accessToken) => {
  const db = await connect();
  const token = await db.oauth_tokens.findOne({ accessToken });

  let result;

  if (token) {
    result = {
      accessToken: token.accessToken,
      accessTokenExpiresAt: token.accessTokenExpiresAt,
      client: {
        id: token.clientId,
      },
      expires: token.accessTokenExpiresAt,
      user: {
        id: token.userId,
      },
    };
  }

  return result;
};

module.exports.getUserFromClient = async (client) => {
  const db = await connect();
  const oauthClient = await db.oauth_clients.findOne({
    id: client.clientId,
    userId: client.userId,
  });

  let result;
  if (oauthClient) {
    result = await db.users.findOne({ id: oauthClient.userId });
  }

  return result;
};

module.exports.getClient = async (clientId, clientSecret) => {
  const db = await connect();
  const query = {
    clientId,
  };
  if (clientSecret) {
    query.clientSecret = clientSecret;
  }

  const client = await db.oauth_clients.findOne(query);

  let result;
  if (client) {
    result = {
      clientSecret: client.clientSecret,
      grants: client.grants,
      id: client.clientId,
      userId: client.userId,
    };
  }

  return result;
};

module.exports.getRefreshToken = async (refreshToken) => {
  const db = await connect();
  const token = await db.oauth_tokens.findOne({ refreshToken });

  return {
    refreshToken: token.refreshToken,
    client: {
      id: token.clientId,
    },
    user: {
      id: token.userId,
    },
  };
};

module.exports.getUser = async (email, password) => {
  const db = await connect();
  let result;
  const user = await db.users.findOne({ email });

  let isValid = false;
  if (user) {
    isValid = await argon2.verify(user.password, password);
    if (isValid) {
      result = Object.assign({}, user, { password: null });
    }
  }

  return result;
};

module.exports.saveToken = async (token, client, user) => {
  const db = await connect();
  const inserted = await db.oauth_tokens.insert({
    accessToken: token.accessToken,
    accessTokenExpiresAt: token.accessTokenExpiresAt,
    clientId: client.id,
    refreshToken: token.refreshToken,
    refreshTokenExpiresAt: token.refreshTokenExpiresAt,
    userId: user.id,
  });

  let result;

  if (inserted) {
    result = {
      accessToken: token.accessToken,
      accessTokenExpiresAt: token.accessTokenExpiresAt,
      client: {
        id: client.id,
      },
      refreshToken: token.refreshToken,
      refreshTokenExpiresAt: token.refreshTokenExpiresAt,
      user: {
        id: user.id,
      },
    };
  }

  return result;
};

module.exports.revokeToken = async (token) => {
  const db = await connect();
  const result = await db.oauth_tokens.destroy({
    refreshToken: token.refreshToken,
  });

  return result;
};

module.exports.saveAuthorizationCode = () => false;
var fastify = require('fastify');
var oauthserver = require('fastify-oauth-server');

var app = fastify();

app.register(oauthserver, {
    accessTokenLifetime: 4 * 60 * 60, // access token liftime in seconds
    model: require('./model'), // oauth2-server model with implemented methods for desired grants
    requireClientAuthentication: { // you can disable clientSecret requirement for different types of grants
    refresh_token: false,  // disable clientSecret requirement for refresh_token grant
    password: false, // disable clientSecret requirement for password grant
    ... // and so on
    },
    skipResponse: true, // do not use fastify-oauth-server's handleResponse function
});

app.post('/oauth/token', async (req, reply) => {
    const token = await req.oauth.token(req, reply);
    reply.send(token);
});

app.get('/status', async (req, reply) => {
    try {
      const token = await req.oauth.authenticate(req, reply);
      console.log(token); // will contain w/e you return from model's getAccessToken method
      reply.code(200).send({ status: 'ok' });
    } catch (e) {
      reply.code(401).send({
        errors: {
          error: e.toString(),
        },
      });
    }
});

app.listen(3000);