Skip to content

Commit

Permalink
fix: auth({ type: "installation", factor }) for token authentication (
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Oct 28, 2020
1 parent cbeb64e commit efcb852
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const octokit = new ProbotOctokit({
});
```

**Note**: `octokit.auth()` will always resolve with an [`oauth` authentication object](https://github.com/octokit/auth-token.js#authentication-object), no matter what options you will pass.
**Note**: when using `octokit.auth({ type: "installation", factory })`, `factory(options)` will be called with `options.octokit`, `options.octokitOptions`, plus any other properties that have been passed to `octokit.auth()` besides `type` and `factory`. In all other cases, `octokit.auth()` will resolve with an [`oauth` authentication object](https://github.com/octokit/auth-token.js#authentication-object), no matter the passed options.

### App authentication

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "ISC",
"dependencies": {
"@octokit/auth-app": "^2.9.0",
"@octokit/auth-app": "^2.10.0",
"@octokit/auth-token": "^2.4.2",
"@octokit/auth-unauthenticated": "^1.0.0",
"@octokit/types": "^5.5.0"
Expand Down
15 changes: 15 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import { AuthOptions, State } from "./types";
export async function auth(state: State, options: AuthOptions) {
// return authentication from internal auth instance unless the event is "event-octokit"
if (options.type !== "event-octokit") {
if (
state.type === "token" &&
options.type === "installation" &&
options.factory
) {
const { type, factory, ...factoryAuthOptions } = options;
return factory(
// @ts-ignore factory options differ if internal auth type is token
Object.assign({}, factoryAuthOptions, {
octokit: state.octokit,
octokitOptions: state.octokitOptions,
})
);
}

return state.auth(options);
}

Expand Down
3 changes: 2 additions & 1 deletion src/get-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export function getState(options: StrategyOptions): State {
octokit: options.octokit,
octokitOptions: options.octokitOptions,
};

if ("token" in options) {
return {
type: "token",
auth: createTokenAuth(options.token),
auth: createTokenAuth(String(options.token)),
...common,
};
}
Expand Down
24 changes: 0 additions & 24 deletions test/auth-test.test.ts

This file was deleted.

47 changes: 47 additions & 0 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Octokit } from "@octokit/core";

import { createProbotAuth } from "../src";

const ProbotOctokit = Octokit.defaults({
authStrategy: createProbotAuth,
});

describe("octokit.auth()", () => {
describe("Token authentication", () => {
test(".auth({ type: 'app' })", async () => {
const octokit = new ProbotOctokit({
auth: {
token: "secret123",
},
});

const authentication = await octokit.auth({ type: "app" });
expect(authentication).toStrictEqual({
type: "token",
token: "secret123",
tokenType: "oauth",
});
});

test(".auth({ type: 'installation', factory })", async () => {
const octokitOptions = {
auth: {
token: "secret123",
},
};
const octokit = new ProbotOctokit(octokitOptions);

const factory = jest.fn().mockReturnValue("test");

const authentication = await octokit.auth({
type: "installation",
factory,
});
expect(authentication).toEqual("test");
expect(factory).toBeCalledWith({
octokit,
octokitOptions,
});
});
});
});

0 comments on commit efcb852

Please sign in to comment.