Coder Social home page Coder Social logo

sleepthegod / passport-snyk-oauth2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snyk/passport-snyk-oauth2

0.0 0.0 0.0 304 KB

Snyk Apps Passport OAuth2 Strategy

Home Page: https://github.com/snyk/passport-snyk-oauth2

License: MIT License

JavaScript 5.03% TypeScript 94.97%

passport-snyk-oauth2's Introduction

@snyk/passport-snyk-oauth2

Snyk's OAuth2 strategy for Passportjs to make authenticating Snyk Apps seamless.

Intro

We recently launched Snyk Apps which allows developers to create their own apps for Snyk and extend the functionality of the Snyk platform. It is available for all languages or framework of your choice. We used Node.js and TypeScript to demo the authencition flow and usage of Snyk Apps using our Snyk App Demo. In the Snyk App Demo we use Passportjs to make the authentication flow and implementation of the same easier for the user. To further extend this, we have created @snyk/passport-snyk-oauth2. This can be easily integrated with Passportjs and make your developer experience even better.

Usage

Install

npm install @snyk/passport-snyk-oauth2
// or
yarn add @snyk/passport-snyk-oauth2

Configure Strategy

import axios from 'axios';
import passport from 'passport';
import SnykOAuth2Strategy from '@snyk/passport-snyk-oauth2';

// User can pass their own implementation of fetching the profile
// by providing the profileFunc implementation. Snyk OAuth2 strategy
// will call this function to fetch the profile associated with request
const profileFunc: ProfileFunc = function (accessToken: string) {
    return axios.get('https://api.dev.snyk.io/v1/user/me', {
      headers: { 'Content-Type': 'application/json; charset=utf-8', Authorization: `bearer ${accessToken}` },
    });
};

passport.use(
    new SnykOAuth2Strategy(
      {
        authorizationURL: testData.authorizationURL,
        tokenURL: testData.tokenURL,
        clientID: testData.clientID,
        clientSecret: testData.clientSecret,
        callbackURL: testData.callbackURL,
        scope: testData.scope,
        scopeSeparator: ' ',
        state: true,
        passReqToCallback: true,
        nonce: testData.nonce,
        profileFunc: fetchProfile,
      },
      // Callback function called with the
      // data fetched as part of authentication
      async function (
        req: Request,
        access_token: string,
        refresh_token: string,
        params: Params,
        profile: any,
        done: any,
      ) {
        // Notify passport that all work, like storing
        // of data in DB has been completed
        done(null, 'Done!');
      },
    ),
);

Authentication Requests

import express from 'express';
const app = express();

app.get('/auth', passport.authenticate('snyk-oauth2'));

app.get(
    '/callback',
    passport.authenticate('snyk-oauth2', {
      successRedirect: '/callback/success',
      failureRedirect: '/callback/failure',
    }),
  );
app.get('/callback/success', (req, res) => {
    return res.send('Authenticated successfull');
  });

app.get('/callback/failure', (req, res) => {
    return res.send('Authentication failed');
  });

passport-snyk-oauth2's People

Contributors

love-bhardwaj avatar mitechie 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.