@hono-dev/auth-github
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Logo

@hono-dev/auth-github

For more details: https://github.com/willin/sso

Useage

GitHub provides two types of Apps to utilize its API: the GitHub App and the OAuth App. To understand the differences between these apps, you can read this article from GitHub, helping you determine the type of App you should select.

Parameters

  • client_id:
    • Type: string.
    • Required.
    • Github App and Oauth App.
    • Your app client ID. You can find this value in the GitHub App settings or the OAuth App settings based on your App type.
      When developing Cloudflare Workers, there's no need to send this parameter. Just declare it in the .dev.vars file as GITHUB_ID=.
  • client_secret:
    • Type: string.
    • Required.
    • Github App and Oauth App.
    • Your app client secret. You can find this value in the GitHub App settings or the OAuth App settings based on your App type.
      When developing Cloudflare Workers, there's no need to send this parameter. Just declare it in the .dev.vars file as GITHUB_SECRET=.

      Do not share your client secret to ensure the security of your app.

  • scope:
    • Type: string[] | string.
    • Optional.
    • Oauth App.
    • Set of permissions to request the user's authorization to access your app for retrieving user information and performing actions on their behalf.
      Review all the scopes Github offers for utilizing their API on the Permissions page.
      For GitHub Apps, you select the scopes during the App creation process or in the settings.
  • oauthApp:
    • Type: boolean.
    • Optional.
    • Oauth App.
    • Set this value to true if your App is of the OAuth App type. Defaults to false.
  • redirect_uri:
    • Type: string.
    • Optional.
    • Oauth App.
    • Github can have multiple callback URLs. Defaults to c.req.url.
      When developing Cloudflare Workers, there's no need to send this parameter. Just declare it in the .dev.vars file as GITHUB_CALLBACK_URL=.

Authentication Flow

After the completion of the Github Auth flow, essential data has been prepared for use in the subsequent steps that your app needs to take.

githubAuth method provides 2 set key data:

  • github-token:
    • Access token to make requests to the Github API for retrieving user information and performing actions on their behalf.
    • Type:
      {
        access_token: string;
        expires_in?: number;  // -> only available for Oauth Apps
        refresh_token?: string;
        refresh_token_expires_in?: number;
        token_type: string;
        scope: GitHubScope[]; // -> Granted Scopes
      }
  • github-user:
    • User basic info retrieved from Github
    • Type:
      {
        login:  string
        id:  number
        node_id:  string
        avatar_url:  string
        gravatar_id:  string
        url:  string
        html_url:  string
        followers_url:  string
        following_url:  string
        gists_url:  string
        starred_url:  string
        subscriptions_url:  string
        organizations_url:  string
        repos_url:  string
        events_url:  string
        received_events_url:  string
        type:  string
        site_admin:  boolean
        name:  string
        company:  string
        blog:  string
        location:  string
        email:  string  |  null
        hireable:  boolean  |  null
        bio:  string
        twitter_username:  string
        public_repos:  number
        public_gists:  number
        followers:  number
        following:  number
        created_at:  string
        updated_at:  string
        private_gists:  number, // -> Github App
        total_private_repos:  number, // -> Github App
        owned_private_repos:  number, // -> Github App
        disk_usage:  number, // -> Github App
        collaborators:  number, // -> Github App
        two_factor_authentication:  boolean, // -> Github App
        plan: {
          name:  string,
          space:  number,
          collaborators:  number,
          private_repos:  number
        } // -> Github App
      }

Github App Example

import { Hono } from 'hono';
import { githubAuth } from '@hono-dev/auth-github';

const app = new Hono();

app.use(
  '/github',
  githubAuth({
    client_id: Bun.env.GITHUB_ID,
    client_secret: Bun.env.GITHUB_SECRET
  })
);

app.get('/github', (c) => {
  const token = c.get('github-token');
  const user = c.get('github-user');

  return c.json({
    token,
    user
  });
});

export default app;

OAuth App Example

import { Hono } from 'hono';
import { githubAuth } from '@hono-dev/auth-github';

const app = new Hono();

app.use(
  '/github',
  githubAuth({
    client_id: Bun.env.GITHUB_ID,
    client_secret: Bun.env.GITHUB_SECRET,
    scope: ['public_repo', 'read:user', 'user', 'user:email', 'user:follow'],
    oauthApp: true
  })
);

app.get('/github', (c) => {
  const token = c.get('github-token');
  const user = c.get('github-user');

  return c.json({
    token,
    user
  });
});

export default app;

赞助 Sponsor

维护者 Owner: Willin Wang

如果您对本项目感兴趣,可以通过以下方式支持我:

Donation ways:

许可证 License

Apache-2.0

Package Sidebar

Install

npm i @hono-dev/auth-github

Weekly Downloads

2

Version

0.0.1

License

Apache-2.0

Unpacked Size

25.9 kB

Total Files

8

Last publish

Collaborators

  • willin