@octokit/oauth-authorization-url
Advanced tools
Comparing version 4.1.2 to 4.2.0
@@ -5,5 +5,56 @@ 'use strict'; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
function oauthAuthorizationUrl(options) { | ||
const scopesNormalized = typeof options.scopes === "string" ? options.scopes.split(/[,\s]+/).filter(Boolean) : Array.isArray(options.scopes) ? options.scopes : []; | ||
const result = { | ||
const clientType = options.clientType || "oauth-app"; | ||
const baseUrl = options.baseUrl || "https://github.com"; | ||
const common = { | ||
allowSignup: options.allowSignup === false ? false : true, | ||
@@ -13,7 +64,11 @@ clientId: options.clientId, | ||
redirectUrl: options.redirectUrl || null, | ||
scopes: scopesNormalized, | ||
state: options.state || Math.random().toString(36).substr(2), | ||
url: "" | ||
}; | ||
const baseUrl = options.baseUrl || "https://github.com"; | ||
const result = clientType === "oauth-app" ? _objectSpread2(_objectSpread2({}, common), {}, { | ||
clientType: "oauth-app", | ||
scopes: scopesNormalized | ||
}) : _objectSpread2(_objectSpread2({}, common), {}, { | ||
clientType: "github-app" | ||
}); | ||
result.url = urlBuilderAuthorize(`${baseUrl}/login/oauth/authorize`, result); | ||
@@ -33,5 +88,10 @@ return result; | ||
let url = base; | ||
Object.entries(options).filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array | ||
.map(([key]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
Object.keys(map).filter(k => options[k] !== null) // Filter out keys that are null and remove the url key | ||
.filter(k => { | ||
if (k !== "scopes") return true; | ||
if (options.clientType === "github-app") return false; | ||
return !Array.isArray(options[k]) || options[k].length > 1; | ||
}) // Filter out empty scopes array | ||
// @ts-ignore | ||
.map(key => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
.forEach(([key, value], index) => { | ||
@@ -38,0 +98,0 @@ // Finally, build the URL |
@@ -7,3 +7,5 @@ export function oauthAuthorizationUrl(options) { | ||
: []; | ||
const result = { | ||
const clientType = options.clientType || "oauth-app"; | ||
const baseUrl = options.baseUrl || "https://github.com"; | ||
const common = { | ||
allowSignup: options.allowSignup === false ? false : true, | ||
@@ -13,7 +15,15 @@ clientId: options.clientId, | ||
redirectUrl: options.redirectUrl || null, | ||
scopes: scopesNormalized, | ||
state: options.state || Math.random().toString(36).substr(2), | ||
url: "", | ||
}; | ||
const baseUrl = options.baseUrl || "https://github.com"; | ||
const result = clientType === "oauth-app" | ||
? { | ||
...common, | ||
clientType: "oauth-app", | ||
scopes: scopesNormalized, | ||
} | ||
: { | ||
...common, | ||
clientType: "github-app", | ||
}; | ||
result.url = urlBuilderAuthorize(`${baseUrl}/login/oauth/authorize`, result); | ||
@@ -32,6 +42,13 @@ return result; | ||
let url = base; | ||
Object.entries(options) | ||
.filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => (Array.isArray(v) ? v.length !== 0 : true)) // Filter out empty Array | ||
.map(([key]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
Object.keys(map) | ||
.filter((k) => options[k] !== null) // Filter out keys that are null and remove the url key | ||
.filter((k) => { | ||
if (k !== "scopes") | ||
return true; | ||
if (options.clientType === "github-app") | ||
return false; | ||
return !Array.isArray(options[k]) || options[k].length > 1; | ||
}) // Filter out empty scopes array | ||
// @ts-ignore | ||
.map((key) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
.forEach(([key, value], index) => { | ||
@@ -38,0 +55,0 @@ // Finally, build the URL |
@@ -1,2 +0,2 @@ | ||
import { Options, Result } from "./types"; | ||
export declare function oauthAuthorizationUrl(options: Options): Result; | ||
import { ClientType, Options, Result } from "./types"; | ||
export declare function oauthAuthorizationUrl<TClientType extends ClientType = "oauth-app">(options: Options<TClientType>): Result<TClientType>; |
@@ -1,3 +0,5 @@ | ||
export interface Options { | ||
export declare type ClientType = "oauth-app" | "github-app"; | ||
export declare type OAuthAppOptions<TClientType extends "oauth-app"> = { | ||
clientId: string; | ||
clientType?: TClientType; | ||
allowSignup?: boolean; | ||
@@ -9,6 +11,19 @@ login?: string; | ||
baseUrl?: string; | ||
} | ||
export interface Result { | ||
}; | ||
export declare type GitHubAppOptions<TClientType extends "github-app"> = { | ||
clientId: string; | ||
clientType: TClientType; | ||
/** `scopes` are not permitted for GitHub Apps */ | ||
scopes?: never; | ||
allowSignup?: boolean; | ||
login?: string; | ||
redirectUrl?: string; | ||
state?: string; | ||
baseUrl?: string; | ||
}; | ||
export declare type Options<TClientType extends ClientType> = TClientType extends "oauth-app" ? OAuthAppOptions<TClientType> : TClientType extends "github-app" ? GitHubAppOptions<TClientType> : never; | ||
declare type OAuthAppResult<TClientType extends "oauth-app"> = { | ||
allowSignup: boolean; | ||
clientId: string; | ||
clientType: TClientType; | ||
login: string | null; | ||
@@ -19,3 +34,13 @@ redirectUrl: string | null; | ||
url: string; | ||
} | ||
export declare type ResultKeys = Exclude<keyof Result, "url">; | ||
}; | ||
declare type GitHubAppResult<TClientType extends "github-app"> = { | ||
allowSignup: boolean; | ||
clientId: string; | ||
clientType: TClientType; | ||
login: string | null; | ||
redirectUrl: string | null; | ||
state: string; | ||
url: string; | ||
}; | ||
export declare type Result<TClientType extends ClientType> = TClientType extends "oauth-app" ? OAuthAppResult<TClientType> : TClientType extends "github-app" ? GitHubAppResult<TClientType> : never; | ||
export {}; |
@@ -7,3 +7,5 @@ function oauthAuthorizationUrl(options) { | ||
: []; | ||
const result = { | ||
const clientType = options.clientType || "oauth-app"; | ||
const baseUrl = options.baseUrl || "https://github.com"; | ||
const common = { | ||
allowSignup: options.allowSignup === false ? false : true, | ||
@@ -13,7 +15,15 @@ clientId: options.clientId, | ||
redirectUrl: options.redirectUrl || null, | ||
scopes: scopesNormalized, | ||
state: options.state || Math.random().toString(36).substr(2), | ||
url: "", | ||
}; | ||
const baseUrl = options.baseUrl || "https://github.com"; | ||
const result = clientType === "oauth-app" | ||
? { | ||
...common, | ||
clientType: "oauth-app", | ||
scopes: scopesNormalized, | ||
} | ||
: { | ||
...common, | ||
clientType: "github-app", | ||
}; | ||
result.url = urlBuilderAuthorize(`${baseUrl}/login/oauth/authorize`, result); | ||
@@ -32,6 +42,13 @@ return result; | ||
let url = base; | ||
Object.entries(options) | ||
.filter(([k, v]) => v !== null && k !== "url") // Filter out keys that are null and remove the url key | ||
.filter(([, v]) => (Array.isArray(v) ? v.length !== 0 : true)) // Filter out empty Array | ||
.map(([key]) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
Object.keys(map) | ||
.filter((k) => options[k] !== null) // Filter out keys that are null and remove the url key | ||
.filter((k) => { | ||
if (k !== "scopes") | ||
return true; | ||
if (options.clientType === "github-app") | ||
return false; | ||
return !Array.isArray(options[k]) || options[k].length > 1; | ||
}) // Filter out empty scopes array | ||
// @ts-ignore | ||
.map((key) => [map[key], `${options[key]}`]) // Map Array with the proper URL parameter names and change the value to a string using template strings | ||
.forEach(([key, value], index) => { | ||
@@ -38,0 +55,0 @@ // Finally, build the URL |
{ | ||
"name": "@octokit/oauth-authorization-url", | ||
"description": "Universal library to retrieve GitHub’s identity URL for the OAuth web flow", | ||
"version": "4.1.2", | ||
"description": "Universal library to retrieve GitHubâs identity URL for the OAuth web flow", | ||
"version": "4.2.0", | ||
"license": "MIT", | ||
@@ -17,7 +17,3 @@ "files": [ | ||
], | ||
"homepage": "https://github.com/octokit/oauth-authorization-url.js#readme", | ||
"bugs": { | ||
"url": "https://github.com/octokit/oauth-authorization-url.js/issues" | ||
}, | ||
"repository": "https://github.com/octokit/oauth-authorization-url.js", | ||
"repository": "github:octokit/oauth-authorization-url.js", | ||
"dependencies": {}, | ||
@@ -24,0 +20,0 @@ "devDependencies": { |
@@ -6,6 +6,17 @@ # oauth-authorization-url.js | ||
[![@latest](https://img.shields.io/npm/v/@octokit/oauth-authorization-url.svg)](https://www.npmjs.com/package/@octokit/oauth-authorization-url) | ||
[![Build Status](https://travis-ci.com/octokit/oauth-authorization-url.js.svg?branch=master)](https://travis-ci.com/octokit/oauth-authorization-url.js) | ||
[![Build Status](https://github.com/octokit/oauth-authorization-url.js/workflows/Test/badge.svg)](https://github.com/octokit/oauth-authorization-url.js/actions?query=workflow%3ATest+branch%3Amaster) | ||
See [GitHub’s Developer Guide for the OAuth web application flow](https://developer.github.com/enterprise/2.16/apps/building-oauth-apps/authorizing-oauth-apps/#1-request-a-users-github-identity). | ||
See [GitHub’s Developer Guide for the OAuth App web application flow](https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow). Note that the [OAuth web application flow for GitHub Apps](https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps#web-application-flow) is slightly different. GitHub Apps do not support scopes for its user access tokens (they are called user-to-server tokens for GitHub Apps), instead they inherit the user permissions from the GitHub App's registration and the repository/organization access and permissions from the respective installation. | ||
<!-- toc --> | ||
- [Usage](#usage) | ||
- [For OAuth Apps](#for-oauth-apps) | ||
- [For GitHub Apps](#for-github-apps) | ||
- [Options](#options) | ||
- [Result](#result) | ||
- [License](#license) | ||
<!-- tocstop --> | ||
## Usage | ||
@@ -47,2 +58,4 @@ | ||
### For OAuth Apps | ||
```js | ||
@@ -57,2 +70,3 @@ const { | ||
} = oauthAuthorizationUrl({ | ||
clientType: "oauth-app", | ||
clientId: "1234567890abcdef1234", | ||
@@ -66,2 +80,14 @@ redirectUrl: "https://example.com", | ||
### For GitHub Apps | ||
```js | ||
const { url, clientId, redirectUrl, login, state } = oauthAuthorizationUrl({ | ||
clientType: "github-app", | ||
clientId: "lv1.1234567890abcdef", | ||
redirectUrl: "https://example.com", | ||
login: "octocat", | ||
state: "secret123", | ||
}); | ||
``` | ||
## Options | ||
@@ -91,2 +117,12 @@ | ||
<th> | ||
<code>clientType</code> | ||
</th> | ||
<td> | ||
Must be set to either `"oauth-app"` or `"github-app"`. Defaults to `"oauth-app"`. | ||
</td> | ||
</tr> | ||
<tr> | ||
<th> | ||
<code>redirectUrl</code> | ||
@@ -111,4 +147,10 @@ </th> | ||
<td> | ||
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. | ||
</td> | ||
Only relevant when `clientType` is set to `"oauth-app"`. | ||
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. | ||
Defaults to `[]` if `clientType` is set to `"oauth-app"`. | ||
</td> | ||
</tr> | ||
@@ -129,3 +171,3 @@ <tr> | ||
<td> | ||
Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. The default is <code>true</code>. Use <code>false</code> in the case that a policy prohibits signups. | ||
Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. Use <code>false</code> in the case that a policy prohibits signups. Defaults to <code>true</code>. | ||
</td> | ||
@@ -170,2 +212,10 @@ </tr> | ||
<th> | ||
<code>clientType</code> | ||
</th> | ||
<td> | ||
Returns <code>options.clientType</code>. Defaults to <code>"oauth-app"</code>. | ||
</td> | ||
</tr> | ||
<tr> | ||
<th> | ||
<code>clientId</code> | ||
@@ -198,4 +248,8 @@ </th> | ||
<td> | ||
Always returns an array of strings. Returns <code>options.scopes</code> if it was set and turns the string into an array if a string was passed. Defaults to <code>[]</code>. | ||
</td> | ||
Only set if `options.clientType` is set to `"oauth-app"`. | ||
Returns an array of strings. Returns <code>options.scopes</code> if it was set and turns the string into an array if a string was passed, otherwise <code>[]</code>. | ||
</td> | ||
</tr> | ||
@@ -202,0 +256,0 @@ <tr> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
27474
254
269
2
1