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

@idio/github

v1.5.1

Published

The GitHub OAuth Flow For Idio Web Server.

Downloads

65

Readme

@idio/github

npm version

@idio/github is The GitHub OAuth Flow For the Idio Web Server.

yarn add @idio/github

Table Of Contents

API

The package is available by importing its default function:

import github from '@idio/github'

githubOAuth(  app: _goa.Application,  config: !GithubOAuthConfig,): void

The GitHub OAuth Login Routes For The Idio Web Server. Two routes will be configured: one to redirect to GitHub to start authentication, and one to handle the callback from GitHub. They will be installed on the app automatically.

  • app* _goa.Application: The Goa/Koa Application.
  • config* !GithubOAuthConfig: Options for the middleware.

GithubOAuthConfig: Options for the program.

import github from '..'
import idio from '@idio/idio'

const Server = async () => {
  const { url, app, router, middleware: {
    session,
  } } = await idio({
    session: {
      keys: [process.env.SESSION_KEY],
    },
  })

  router.get('/', session, (ctx) => {
    ctx.body = render(<html>
      <body>
        {ctx.session.user ?
          <span>Hello, {ctx.session.user}.{' '}
            <a href="/signout">Sign Out</a>
          </span> :
          <a href="/github">Sign In With GitHub</a>}
        <hr/>
        (c) Art Deco, 2020
      </body>
    </html>, { addDoctype: true })
  })
  router.get('/signout', session, (ctx) => {
    ctx.session = null
    ctx.redirect('/')
  })
  app.use(router.routes())

  github(app, {
    session,
    client_id: process.env.CLIENT_ID,
    client_secret: process.env.CLIENT_SECRET,
    scope: 'user:email',
    error(ctx, error) {
      ctx.redirect(`/?error=${error}`)
    },
    path: '/github',
    async finish(ctx, token, scope, user, next) {
      console.log(user.name, user.login, user.company)
      ctx.session.user = user.login
      ctx.session.manuallyCommit()
      ctx.redirect('/')
    },
  })
  return { app, url }
}
[+] CLIENT_ID [+] CLIENT_SECRET [+] SESSION_KEY 
http://localhost:5000 
{
  body: 'Redirecting to <a href="https://www.github.com/login/oauth/authorize?client_id=f0a8762e7329780e85de&amp;redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fgithub%2Fredirect&amp;state=8275&amp;scope=user%3Aemail">https://www.github.com/login/oauth/authorize?client_id=f0a8762e7329780e85de&amp;redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fgithub%2Fredirect&amp;state=8275&amp;scope=user%3Aemail</a>.',
  headers: {
    'set-cookie': [
      'koa:sess=eyJnaXRoaWItc3RhdGUiOjgyNzUsIl9leHBpcmUiOjE1ODI4OTY0NTk1OTIsIl9tYXhBZ2UiOjg2NDAwMDAwfQ==; path=/; expires=Fri, 28 Feb 2020 13:27:39 GMT; httponly',
      'koa:sess.sig=mPZuFR0kFz8XFkQRJeuTm3VnMfw; path=/; expires=Fri, 28 Feb 2020 13:27:39 GMT; httponly'
    ],
    location: 'https://www.github.com/login/oauth/authorize?client_id=f0a8762e7329780e85de&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fgithub%2Fredirect&state=8275&scope=user%3Aemail',
    'content-type': 'text/html; charset=utf-8',
    'content-length': '391',
    date: 'Thu, 27 Feb 2020 13:27:39 GMT',
    connection: 'close'
  },
  statusCode: 302,
  statusMessage: 'Found'
}

 > Redirect to Dialog https://www.github.com/login/oauth/authorize?client_id=f0a8762e7329780e85de&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fgithub%2Fredirect&state=8275&scope=user%3Aemail

If authorisation was successful, the server will make a request to GitHub API at /user path with the token, to get user's public info. This information can then be accessed in the finish function passed in the config.

If the user:email scope was requested, emails returned from the /user/emails API path will also be populated in the emails field. If the user's main email is private, it won't be visible in the email field, so that this scope should be requested if the email address needs to be collected.

GithubEmail

GithubUser: Public user information

A custom implementation of the finish function can be provided, only that session must be manually committed after being set.

/**
 * @param {!_idio.Context} ctx
 * @param {string} token
 * @param {string} scope
 * @param {!_idio.GithubUser} user
 */
export const defaultFinish = async (ctx, token, scope, user, next) => {
  ctx.session['token'] = token
  ctx.session['scope'] = scope
  ctx.session['user'] = user
  await ctx.session.manuallyCommit()
  ctx.redirect('/')
}

Copyright

GNU Affero General Public License v3.0

Affero GPL means that you're not allowed to use this middleware on the web unless you release the source code for your application. This is a restrictive license which has the purpose of defending Open Source work and its creators.

Please refer to the Idio license agreement for more info on dual-licensing. You're allowed to use this middleware without disclosing the source code if you sign up on the neoluddite.dev package reward scheme.