Skip to content

Commit ca667b9

Browse files
authored
feat: deprecate auth({ type: "token" }), code, state, redirectUrl strategy options, and setting user authentication in request hook (#163)
1 parent 9a2f460 commit ca667b9

File tree

6 files changed

+92
-16
lines changed

6 files changed

+92
-16
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ It implements authentication using an OAuth app’s client ID and secret as well
1515
- [`createOAuthAppAuth(options)`](#createoauthappauthoptions)
1616
- [`auth(options)`](#authoptions)
1717
- [Authentication object](#authentication-object)
18-
- [OAuth authentication](#oauth-authentication)
19-
- [OAuth access token authentication](#oauth-access-token-authentication)
18+
- [OAuth authentication](#oauth-authentication)
19+
- [OAuth access token authentication](#oauth-access-token-authentication)
2020
- [`auth.hook(request, route, parameters)` or `auth.hook(request, options)`](#authhookrequest-route-parameters-or-authhookrequest-options)
2121
- [Implementation details](#implementation-details)
2222
- [License](#license)
@@ -76,8 +76,8 @@ const appAuthentication = await auth({
7676
// }
7777
// }
7878

79-
const tokenAuthentication = await auth({
80-
type: "token",
79+
const userAuthentication = await auth({
80+
type: "oauth-user",
8181
code: "random123", // code from OAuth web flow, see https://git.io/fhd1D
8282
state: "mystate123",
8383
});
@@ -216,7 +216,7 @@ The async `auth()` method returned by `createOAuthAppAuth(options)` accepts the
216216
<code>string</code>
217217
</th>
218218
<td>
219-
<strong>Required.</strong> Either <code>"oauth-app"</code> or <code>"token"</code>.
219+
<strong>Required.</strong> Either <code>"oauth-app"</code> or <code>"oauth-user"</code>.
220220
</td>
221221
</tr>
222222
<tr>

src/auth.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
import btoa from "btoa-lite";
22

33
import { getOAuthAccessToken } from "./get-oauth-access-token";
4-
import { State, AuthOptions, Authentication } from "./types";
4+
import {
5+
State,
6+
AuthAppOptions,
7+
AuthTokenOptions,
8+
DeprecatedAuthTokenOptions,
9+
Authentication,
10+
} from "./types";
511

612
export async function auth(
713
state: State,
8-
authOptions: AuthOptions
14+
authOptions: AuthAppOptions
15+
): Promise<Authentication>;
16+
17+
export async function auth(
18+
state: State,
19+
authOptions: AuthTokenOptions
20+
): Promise<Authentication>;
21+
22+
/**
23+
* @deprecated `type: "token"` is deprecate. Use `type: "oauth"` instead
24+
*/
25+
export async function auth(
26+
state: State,
27+
authOptions: DeprecatedAuthTokenOptions
28+
): Promise<Authentication>;
29+
30+
export async function auth(
31+
state: State,
32+
authOptions: AuthAppOptions | AuthTokenOptions | DeprecatedAuthTokenOptions
933
): Promise<Authentication> {
1034
if (authOptions.type === "token") {
35+
console.warn(
36+
`[@octokit/auth-oauth-app] "{type: 'token'}" is deprecated, use "{type: 'oauth-user'}" instead`
37+
);
38+
}
39+
40+
if (authOptions.type === "token" || authOptions.type === "oauth-user") {
1141
const { token, scopes } = await getOAuthAccessToken(state, {
1242
auth: authOptions,
1343
});

src/get-oauth-access-token.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { RequestError } from "@octokit/request-error";
22

33
import {
44
AuthTokenOptions,
5+
DeprecatedAuthTokenOptions,
56
RequestInterface,
67
State,
78
TokenWithScopes,
@@ -11,7 +12,7 @@ export async function getOAuthAccessToken(
1112
state: State,
1213
options: {
1314
request?: RequestInterface;
14-
auth?: AuthTokenOptions;
15+
auth?: AuthTokenOptions | DeprecatedAuthTokenOptions;
1516
}
1617
): Promise<TokenWithScopes> {
1718
const authOptionsPassed = options.auth

src/hook.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export async function hook(
5858
return response;
5959
}
6060

61+
console.warn(
62+
`[@octokit/auth-oauth-app] setting user authentication is deprecated. Use "@octokit/auth-oauth-user" instead`
63+
);
64+
6165
const { token } = await getOAuthAccessToken(state, { request });
6266
endpoint.headers.authorization = `token ${token}`;
6367

src/index.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { request } from "@octokit/request";
33

44
import { auth } from "./auth";
55
import { hook } from "./hook";
6-
import { StrategyOptions, AuthOptions, Authentication } from "./types";
6+
import {
7+
StrategyOptions,
8+
AuthOptions,
9+
Authentication,
10+
OAuthAppAuthInterface,
11+
} from "./types";
712
import { VERSION } from "./version";
813

914
export type Types = {
@@ -12,19 +17,36 @@ export type Types = {
1217
Authentication: Authentication;
1318
};
1419

15-
export function createOAuthAppAuth(options: StrategyOptions) {
20+
const deprecatedStrategyOptions = ["code", "redirectUrl", "state"];
21+
22+
export function createOAuthAppAuth(
23+
options: StrategyOptions
24+
): OAuthAppAuthInterface {
25+
const usedDeprecatedOptions = deprecatedStrategyOptions.filter(
26+
(option) => option in options
27+
);
28+
29+
if (usedDeprecatedOptions.length) {
30+
console.warn(
31+
`[@octokit/auth-oauth-app] "${usedDeprecatedOptions.join(
32+
", "
33+
)}" strategy options are deprecated. Use "@octokit/auth-oauth-user" instead`
34+
);
35+
}
36+
1637
const state = Object.assign(
1738
{
1839
request: request.defaults({
1940
headers: {
20-
"user-agent": `octokit-auth-oauth-app.js/${VERSION} ${getUserAgent()}`
21-
}
22-
})
41+
"user-agent": `octokit-auth-oauth-app.js/${VERSION} ${getUserAgent()}`,
42+
},
43+
}),
2344
},
2445
options
2546
);
2647

48+
// @ts-expect-error wtf
2749
return Object.assign(auth.bind(null, state), {
28-
hook: hook.bind(null, state)
50+
hook: hook.bind(null, state),
2951
});
3052
}

src/types.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ export type StrategyOptions = {
2020
request?: RequestInterface;
2121
};
2222

23-
type AuthAppOptions = {
23+
export type AuthAppOptions = {
2424
type: "oauth-app";
2525
};
2626
export type AuthTokenOptions = {
27+
type: "oauth-user";
28+
code?: string;
29+
redirectUrl?: string;
30+
state?: string;
31+
};
32+
/** @deprecated type: "token" is deprecated. Use type: "oauth-user" */
33+
export type DeprecatedAuthTokenOptions = {
2734
type: "token";
2835
code?: string;
2936
redirectUrl?: string;
3037
state?: string;
3138
};
3239

33-
export type AuthOptions = AuthAppOptions | AuthTokenOptions;
40+
export type AuthOptions =
41+
| AuthAppOptions
42+
| AuthTokenOptions
43+
| DeprecatedAuthTokenOptions;
3444

3545
export type TokenWithScopes = {
3646
token: string;
@@ -53,3 +63,12 @@ export type State = StrategyOptions & {
5363
request: RequestInterface;
5464
token?: TokenWithScopes;
5565
};
66+
export interface OAuthAppAuthInterface {
67+
(options?: AuthOptions): Promise<Authentication>;
68+
69+
hook(
70+
request: OctokitTypes.RequestInterface,
71+
route: OctokitTypes.Route | OctokitTypes.EndpointOptions,
72+
parameters?: OctokitTypes.RequestParameters
73+
): Promise<OctokitTypes.OctokitResponse<any>>;
74+
}

0 commit comments

Comments
 (0)