Skip to content

Commit 4573ee2

Browse files
committedJan 22, 2020
feat: use @octokit/auth for authentication strategies, deprecate previous auth options
1 parent db35ac8 commit 4573ee2

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed
 

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const Octokit = require("./lib/core");
22

33
const CORE_PLUGINS = [
4-
require("./plugins/log"),
5-
require("./plugins/authentication-deprecated"), // deprecated: remove in v17
64
require("./plugins/authentication"),
5+
require("./plugins/authentication-deprecated"), // deprecated: remove in v17
6+
require("./plugins/log"),
77
require("./plugins/pagination"),
88
require("./plugins/register-endpoints"),
99
require("./plugins/rest-api-endpoints"),

‎plugins/authentication/before-request.js

-12
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ const withAuthorizationPrefix = require("./with-authorization-prefix");
77
function authenticationBeforeRequest(state, options) {
88
if (typeof state.auth === "string") {
99
options.headers.authorization = withAuthorizationPrefix(state.auth);
10-
11-
// https://developer.github.com/v3/previews/#integrations
12-
if (
13-
/^bearer /i.test(state.auth) &&
14-
!/machine-man/.test(options.headers.accept)
15-
) {
16-
const acceptHeaders = options.headers.accept
17-
.split(",")
18-
.concat("application/vnd.github.machine-man-preview+json");
19-
options.headers.accept = acceptHeaders.filter(Boolean).join(",");
20-
}
21-
2210
return;
2311
}
2412

‎plugins/authentication/index.js

+55
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,69 @@
11
module.exports = authenticationPlugin;
22

3+
const { createTokenAuth } = require("@octokit/auth-token");
4+
const { Deprecation } = require("deprecation");
5+
const once = require("once");
6+
37
const beforeRequest = require("./before-request");
48
const requestError = require("./request-error");
59
const validate = require("./validate");
10+
const withAuthorizationPrefix = require("./with-authorization-prefix");
11+
12+
const deprecateAuthBasic = once((log, deprecation) => log.warn(deprecation));
13+
const deprecateAuthObject = once((log, deprecation) => log.warn(deprecation));
614

715
function authenticationPlugin(octokit, options) {
16+
// If `options.authStrategy` is set then use it and pass in `options.auth`
17+
if (options.authStrategy) {
18+
const auth = options.authStrategy(options.auth);
19+
octokit.hook.wrap("request", auth.hook);
20+
octokit.auth = auth;
21+
return;
22+
}
23+
24+
// If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
25+
// is unauthenticated. The `octokit.auth()` method is a no-op and no request hook is registred.
826
if (!options.auth) {
27+
octokit.auth = () =>
28+
Promise.resolve({
29+
type: "unauthenticated"
30+
});
931
return;
1032
}
1133

34+
const isBasicAuthString =
35+
typeof options.auth === "string" &&
36+
/^basic/.test(withAuthorizationPrefix(options.auth));
37+
38+
// If only `options.auth` is set to a string, use the default token authentication strategy.
39+
if (typeof options.auth === "string" && !isBasicAuthString) {
40+
const auth = createTokenAuth(options.auth);
41+
octokit.hook.wrap("request", auth.hook);
42+
octokit.auth = auth;
43+
return;
44+
}
45+
46+
// Otherwise log a deprecation message
47+
const [deprecationMethod, deprecationMessapge] = isBasicAuthString
48+
? [
49+
deprecateAuthBasic,
50+
'Setting the "new Octokit({ auth })" option to a Basic Auth string is deprecated. Use https://github.com/octokit/auth-basic.js instead. See (https://octokit.github.io/rest.js/#authentication)'
51+
]
52+
: [
53+
deprecateAuthObject,
54+
'Setting the "new Octokit({ auth })" option to an object without also setting the "authStrategy" option is deprecated and will be removed in v17. See (https://octokit.github.io/rest.js/#authentication)'
55+
];
56+
deprecationMethod(
57+
octokit.log,
58+
new Deprecation("[@octokit/rest] " + deprecationMessapge)
59+
);
60+
61+
octokit.auth = () =>
62+
Promise.resolve({
63+
type: "deprecated",
64+
message: deprecationMessapge
65+
});
66+
1267
validate(options.auth);
1368

1469
const state = {

0 commit comments

Comments
 (0)
Please sign in to comment.