|
1 | 1 | module.exports = authenticationPlugin;
|
2 | 2 |
|
| 3 | +const { createTokenAuth } = require("@octokit/auth-token"); |
| 4 | +const { Deprecation } = require("deprecation"); |
| 5 | +const once = require("once"); |
| 6 | + |
3 | 7 | const beforeRequest = require("./before-request");
|
4 | 8 | const requestError = require("./request-error");
|
5 | 9 | 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)); |
6 | 14 |
|
7 | 15 | 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. |
8 | 26 | if (!options.auth) {
|
| 27 | + octokit.auth = () => |
| 28 | + Promise.resolve({ |
| 29 | + type: "unauthenticated" |
| 30 | + }); |
9 | 31 | return;
|
10 | 32 | }
|
11 | 33 |
|
| 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 | + |
12 | 67 | validate(options.auth);
|
13 | 68 |
|
14 | 69 | const state = {
|
|
0 commit comments