Gathering detailed insights and metrics for @octokit/oauth-app
Gathering detailed insights and metrics for @octokit/oauth-app
Gathering detailed insights and metrics for @octokit/oauth-app
Gathering detailed insights and metrics for @octokit/oauth-app
npm install @octokit/oauth-app
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
82 Stars
598 Commits
26 Forks
11 Watching
7 Branches
23 Contributors
Updated on 26 Nov 2024
TypeScript (98.08%)
JavaScript (1.92%)
Cumulative downloads
Total Downloads
Last day
-10.6%
112,366
Compared to previous day
Last week
-0.5%
668,554
Compared to previous week
Last month
2.3%
2,954,682
Compared to previous month
Last year
89%
30,728,696
Compared to previous year
8
GitHub OAuth toolset for Node.js
OAuthApp.defaults(options)
app.on(eventName, eventHandler)
app.octokit
app.getUserOctokit(options)
app.getWebFlowAuthorizationUrl(options)
app.createToken(options)
app.checkToken(options)
app.resetToken(options)
app.refreshToken(options)
app.scopeToken(options)
app.deleteToken(options)
app.deleteAuthorization(options)
Browsers |
|
---|---|
Node |
Install with |
[!IMPORTANT] As we use conditional exports, you will need to adapt your
tsconfig.json
by setting"moduleResolution": "node16", "module": "node16"
.See the TypeScript docs on package.json "exports".
See this helpful guide on transitioning to ESM from @sindresorhus
1import { OAuthApp, createNodeMiddleware } from "@octokit/oauth-app"; 2import { createServer } from "node:http"; 3 4const app = new OAuthApp({ 5 clientType: "oauth-app", 6 clientId: "1234567890abcdef1234", 7 clientSecret: "1234567890abcdef1234567890abcdef12345678", 8}); 9 10app.on("token", async ({ token, octokit }) => { 11 const { data } = await octokit.request("GET /user"); 12 console.log(`Token retrieved for ${data.login}`); 13}); 14 15createServer(createNodeMiddleware(app)).listen(3000); 16// can now receive user authorization callbacks at /api/github/oauth/callback 17// See all endpoints at https://github.com/octokit/oauth-app.js#middlewares
GitHub Apps do not support scopes
. If the GitHub App has expiring user tokens enabled, the token used for the octokit
instance will be refreshed automatically, and the additional refresh-related properties will be passed to the "token"
event handler.
1import { OAuthApp, createNodeMiddleware } from "@octokit/oauth-app"; 2import { createServer } from "node:http"; 3 4const app = new OAuthApp({ 5 clientType: "github-app", 6 clientId: "lv1.1234567890abcdef", 7 clientSecret: "1234567890abcdef1234567890abcdef12345678", 8}); 9 10app.on("token", async ({ token, octokit, expiresAt }) => { 11 const { data } = await octokit.request("GET /user"); 12 console.log(`Token retrieved for ${data.login}`); 13}); 14 15createServer(createNodeMiddleware(app)).listen(3000); 16// can now receive user authorization callbacks at /api/github/oauth/callback 17// See all endpoints at https://github.com/octokit/oauth-app.js#middlewares
public/
folder, hosted on Glitch: https://glitch.com/~github-oauth-clientOAuthApp.defaults(options)
Create a new OAuthApp
with custom defaults for the constructor options
1const MyOAuthApp = OAuthApp.defaults({ 2 Octokit: MyOctokit, 3}); 4const app = new MyOAuthApp({ clientId, clientSecret }); 5// app.octokit is now an instance of MyOctokit
name | type | description |
---|---|---|
clientId
|
number
| Required. Find Client ID on the app’s about page in settings. |
clientSecret
|
number
| Required. Find Client Secret on the app’s about page in settings. |
clientType
|
string
|
Either "oauth-app" or "github-app" . Defaults to "oauth-app" .
|
allowSignup
|
boolean
|
Sets the default value for app.getWebFlowAuthorizationUrl(options) .
|
redirectUrl
|
string
| The URL in your application where users will be sent after authorization. See Redirect URLs in GitHub’s Developer Guide. |
defaultScopes
|
Array of strings
|
Only relevant when Sets the default |
log
|
object
|
Used for internal logging. Defaults to console .
|
Octokit
|
Constructor
|
You can pass in your own Octokit constructor with custom defaults and plugins. The Octokit Constructor must use an authentication strategy that is compatible with`@octokit/auth-oauth-app. For usage with enterprise, set
Defaults to |
app.on(eventName, eventHandler)
Called whenever a new OAuth access token is created for a user. It accepts two parameters, an event name and a function with one argument
1app.on("token.created", async (context) => { 2 const { data } = await context.octokit.request("GET /user"); 3 app.log.info(`New token created for ${data.login}`); 4});
The eventName
can be one of (or an array of)
token.created
token.reset
token.refreshed
(GitHub Apps only)token.scoped
(GitHub Apps only)token.deleted
authorization.deleted
All event handlers are awaited before continuing.
context
can have the following properties
property | type | description |
---|---|---|
context.name
|
string
|
Name of the event. One of: token , authorization
|
context.action
|
string
|
Action of the event. One of: created , reset , deleted
|
context.authentication
|
object
|
The OAuth authentication object. See https://github.com/octokit/auth-oauth-user.js/#authentication-object |
context.octokit
|
Octokit instance
|
Authenticated instance using the The |
app.octokit
Octokit instance with OAuth App authentication. Uses Octokit
constructor option
app.getUserOctokit(options)
1const octokit = await app.getUserOctokit({ code: "code123" });
options
are the same as in app.createToken(options)
The octokit
instance is authorized using the user access token if the app is an OAuth app and a user-to-server token if the app is a GitHub app. If the token expires it will be refreshed automatically.
app.getWebFlowAuthorizationUrl(options)
Returns and object with all options and a url
property which is the authorization URL. See https://github.com/octokit/oauth-methods.js/#getwebflowauthorizationurl
1const { url } = app.getWebFlowAuthorizationUrl({ 2 state: "state123", 3 scopes: ["repo"], 4});
name | type | description |
---|---|---|
redirectUrl
|
string
| The URL in your application where users will be sent after authorization. See Redirect URLs in GitHub’s Developer Guide. |
login
|
string
| Suggests a specific account to use for signing in and authorizing the app. |
scopes
|
array of strings
| An array of scope names (or: space-delimited list of scopes). If not provided, scope defaults to an empty list for users that have not authorized any scopes for the application. For users who have authorized scopes for the application, the user won't be shown the OAuth authorization page with the list of scopes. Instead, this step of the flow will automatically complete with the set of scopes the user has authorized for the application. For example, if a user has already performed the web flow twice and has authorized one token with user scope and another token with repo scope, a third web flow that does not provide a scope will receive a token with user and repo scope. |
state
|
string
|
An unguessable random string. It is used to protect against cross-site request forgery attacks.
Defaults to Math.random().toString(36).substr(2) .
|
allowSignup
|
boolean
|
Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. The default is true . Use false in the case that a policy prohibits signups.
|
app.createToken(options)
The method can be used for both, the OAuth Web Flow and the OAuth Device Flow.
For the web flow, you have to pass the code
from URL redirect described in step 2.
1const { token } = await app.createToken({ 2 code: "code123", 3});
name | type | description |
---|---|---|
code
|
string
|
Required. Pass the code that was passed as ?code query parameter in the authorization redirect URL.
|
state
|
string
|
Required. Pass the state that was passed as ?state query parameter in the authorization redirect URL.
|
Resolves with an user authentication object
For the device flow, you have to pass a onVerification
callback function, which prompts the user to enter the received user code at the received authorization URL.
name | type | description |
---|---|---|
onVerification
|
function
|
Required. A function that is called once the device and user codes were retrieved The
|
scopes
|
array of strings
|
Only relevant if Array of OAuth scope names that the user access token should be granted. Defaults to no scopes ( |
Resolves with an user authentication object
app.checkToken(options)
1try { 2 const { created_at, app, user } = await app.checkToken({ token }); 3 console.log( 4 `token valid, created on %s by %s for %s`, 5 created_at, 6 user.login, 7 app.name, 8 ); 9} catch (error) { 10 // token invalid or request error 11}
name | type | description |
---|---|---|
token
|
string
| Required. |
Resolves with response body from "Check a token" request with an additional authentication
property which is a user authentication object.
app.resetToken(options)
1const { data, authentication } = await app.resetToken({ 2 token: "token123", 3}); 4// "token123" is no longer valid. Use `token` instead
name | type | description |
---|---|---|
token
|
string
| Required. |
Resolves with response body from "Reset a token" request with an additional authentication
property which is a user authentication object.
app.refreshToken(options)
Expiring tokens are only supported by GitHub Apps, and only if expiring user tokens are enabled.
1const { data, authentication } = await app.refreshToken({ 2 refreshToken: "refreshtoken123", 3});
name | type | description |
---|---|---|
refreshToken
|
string
| Required. |
Resolves with response body from "Renewing a user token with a refresh token" request (JSON) with an additional authentication
property which is a user authentication object.
app.scopeToken(options)
Scoping a token is only supported by GitHub Apps. "Scoping" in this context means to limit access to a selected installation, with a subset of repositories and permissions.
1const { data, authentication } = await app.scopeToken({ 2 clientType: "github-app", 3 clientId: "lv1.1234567890abcdef", 4 clientSecret: "1234567890abcdef12347890abcdef12345678", 5 token: "usertoken123", 6 target: "octokit", 7 repositories: ["oauth-app.js"], 8 permissions: { 9 issues: "write", 10 }, 11});
Options
name | type | description |
---|---|---|
target
|
string
|
Required unless targetId is set. The name of the user or organization to scope the user-to-server access token to.
|
targetId
|
integer
|
Required unless target is set. The ID of the user or organization to scope the user-to-server access token to.
|
repositories
|
array of strings
|
The list of repository names to scope the user-to-server access token to. repositories may not be specified if repository_ids is specified.
|
repository_ids
|
array of integers
|
The list of repository IDs to scope the user-to-server access token to. repositories may not be specified if repositories is specified.
|
permissions
|
object
| The permissions granted to the user-to-server access token. See GitHub App Permissions. |
Resolves with response body from "Create a scoped access token" request with an additional authentication
property which is a user authentication object.
app.deleteToken(options)
1await app.deleteToken({ 2 token: "token123", 3}); 4// "token123" is no longer valid.
name | type | description |
---|---|---|
token
|
string
| Required. |
Resolves with response body from "Delete a token" request.
app.deleteAuthorization(options)
1await app.deleteAuthorization({ 2 token: "token123", 3}); 4// "token123" is no longer valid, and no tokens can be created until the app gets re-authorized.
name | type | description |
---|---|---|
token
|
string
| Required. |
Resolves with response body from "Delete an app authorization" request.
A middleware is a method or set of methods to handle requests for common environments.
By default, all middlewares expose the following routes
Route | Route Description |
---|---|
GET /api/github/oauth/login | Redirects to GitHub's authorization endpoint. Accepts optional ?state and ?scopes query parameters. ?scopes is a comma-separated list of supported OAuth scope names |
GET /api/github/oauth/callback | The client's redirect endpoint. This is where the token event gets triggered |
POST /api/github/oauth/token | Exchange an authorization code for an OAuth Access token. If successful, the token event gets triggered. |
GET /api/github/oauth/token | Check if token is valid. Must authenticate using token in Authorization header. Uses GitHub's POST /applications/{client_id}/token endpoint |
PATCH /api/github/oauth/token | Resets a token (invalidates current one, returns new token). Must authenticate using token in Authorization header. Uses GitHub's PATCH /applications/{client_id}/token endpoint. |
PATCH /api/github/oauth/refresh-token | Refreshes an expiring token (invalidates current one, returns new access token and refresh token). Must authenticate using token in Authorization header. Uses GitHub's POST https://github.com/login/oauth/access_token OAuth endpoint. |
POST /api/github/oauth/token/scoped | Creates a scoped token (does not invalidate the current one). Must authenticate using token in Authorization header. Uses GitHub's POST /applications/{client_id}/token/scoped endpoint. |
DELETE /api/github/oauth/token | Invalidates current token, basically the equivalent of a logout. Must authenticate using token in Authorization header. |
DELETE /api/github/oauth/grant | Revokes the user's grant, basically the equivalent of an uninstall. must authenticate using token in Authorization header. |
createNodeMiddleware(app, options)
Native http server middleware for Node.js
1import { OAuthApp, createNodeMiddleware } from "@octokit/oauth-app";
2import { createServer } from "node:http";
3
4const app = new OAuthApp({
5 clientType: "oauth-app",
6 clientId: "1234567890abcdef1234",
7 clientSecret: "1234567890abcdef1234567890abcdef12345678",
8});
9
10const middleware = createNodeMiddleware(app, {
11 pathPrefix: "/api/github/oauth",
12});
13
14createServer(middleware).listen(3000);
15// can now receive user authorization callbacks at /api/github/oauth/callback
name | type | description |
---|---|---|
app
|
OAuthApp instance
| Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to |
createWebWorkerHandler(app, options)
Event handler for web worker environments (Cloudflare workers or Deno).
1// worker.js
2import { OAuthApp, createWebWorkerHandler } from "@octokit/oauth-app";
3const app = new OAuthApp({
4 clientType: "oauth-app",
5 clientId: "1234567890abcdef1234",
6 clientSecret: "1234567890abcdef1234567890abcdef12345678",
7});
8
9const handleRequest = createWebWorkerHandler(app, {
10 pathPrefix: "/api/github/oauth",
11});
12
13addEventListener("fetch", (event) => {
14 event.respondWith(handleRequest(event.request));
15});
16// can now receive user authorization callbacks at /api/github/oauth/callback
name | type | description |
---|---|---|
app
|
OAuthApp instance
| Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to |
createAWSLambdaAPIGatewayV2Handler(app, options)
Event handler for AWS Lambda using API Gateway V2 HTTP integration.
1// worker.js
2import {
3 OAuthApp,
4 createAWSLambdaAPIGatewayV2Handler,
5} from "@octokit/oauth-app";
6const app = new OAuthApp({
7 clientType: "oauth-app",
8 clientId: "1234567890abcdef1234",
9 clientSecret: "1234567890abcdef1234567890abcdef12345678",
10});
11
12export const handler = createAWSLambdaAPIGatewayV2Handler(app, {
13 pathPrefix: "/api/github/oauth",
14});
15
16// can now receive user authorization callbacks at /api/github/oauth/callback
name | type | description |
---|---|---|
app
|
OAuthApp instance
| Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to |
When above middlewares do not meet your needs, you can build your own
using the exported handleRequest
function.
handleRequest
function is an abstract HTTP handler which accepts an OctokitRequest
and returns an OctokitResponse
if the request matches any predefined route.
Different environments (e.g., Node.js, Cloudflare Workers, Deno, etc.) exposes different APIs when processing HTTP requests (e.g.,
IncomingMessage
for Node.js,Request
for Cloudflare workers, etc.). Two HTTP-related types (OctokitRequest
andOctokitResponse
) are generalized to make an abstract HTTP handler possible.
To share the behavior and capability with the existing Node.js middleware (and be compatible with OAuth user authentication strategy in the browser), it is better to implement your HTTP handler/middleware based on handleRequest
function.
handleRequest
function takes three parameters:
name | type | description |
---|---|---|
app
|
OAuthApp instance
| Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to |
request
|
OctokitRequest
| Generalized HTTP request in `OctokitRequest` type. |
Implementing an HTTP handler/middleware for a certain environment involves three steps:
IncomingMessage
in Node.js) into an OctokitRequest
object. See node/parse-request.ts
for reference.OctokitResponse
object (e.g., as ServerResponse
in Node.js). See node/send-response.ts
for reference.OctokitRequest
object using handleRequest
.OctokitResponse
object using (2).See CONTRIBUTING.md
No vulnerabilities found.
Reason
all changesets reviewed
Reason
no binaries found in the repo
Reason
17 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
SAST tool is run on all commits
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More