1.5.1 • Published 4 years ago

@idio/github v1.5.1

Weekly downloads
2
License
AGPL-3.0
Repository
github
Last release
4 years ago

@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.

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago