Skip to content

Commit

Permalink
feat: auth({permissions}). permissions and singleFileName (if a…
Browse files Browse the repository at this point in the history
…pplicabale) are now always included in authentication object
  • Loading branch information
gr2m committed Jun 29, 2019
1 parent 3711257 commit 8d54c03
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
58 changes: 53 additions & 5 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { request } from "@octokit/request";

import { StrategyOptionsWithDefaults, AuthOptions } from "./types";
import { StrategyOptionsWithDefaults, AuthOptions, Permissions } from "./types";
import { isAppRoute } from "./is-app-route";
import { toTokenAuthentication } from "./to-token-authentication";
import { toAppAuthentication } from "./to-app-authentication";
Expand All @@ -12,8 +12,36 @@ export async function auth(
if (options) {
const result = state.cache.get(options.installationId);
if (result) {
const [token, expiresAt] = result.split("|");
return toTokenAuthentication(options.installationId, token, expiresAt);
const [
token,
expiresAt,
permissionsString,
repositoryIdsString,
singleFileName
] = result.split("|");

const permissions = permissionsString
.split(/,/)
.reduce((permissions: Permissions, string) => {
if (/!$/.test(string)) {
permissions[string.slice(0, -1)] = "write";
} else {
permissions[string] = "read";
}

return permissions;
}, {});

return toTokenAuthentication(
options.installationId,
token,
expiresAt,
permissions,
repositoryIdsString
? repositoryIdsString.split(",").map(id => parseInt(id, 10))
: undefined,
singleFileName
);
}
}

Expand All @@ -30,22 +58,42 @@ export async function auth(
{
installation_id: options.installationId,
repository_ids: options.repositoryIds,
permissions: options.permissions,
previews: ["machine-man"],
headers: appAuthentication.headers
}
);

const {
data: { permissions, single_file_name: singleFileName }
} = await state.request("GET /app/installations/:installation_id", {
installation_id: options.installationId,
previews: ["machine-man"],
headers: appAuthentication.headers
});

const permissionsString = Object.keys(permissions)
.map(name => `${name}${permissions[name] === "write" ? "!" : ""}`)
.join(",");
// @ts-ignore
const repositoryIds = repositories ? repositories.map(r => r.id) : undefined;
const repositoryIdsString = repositories
? repositoryIds.join(",")
: undefined;

state.cache.set(
options.installationId,
[token, expires_at, repositoryIds].filter(Boolean).join("|")
[token, expires_at, permissionsString, repositoryIdsString, singleFileName]
.join("|")
.replace(/\|+$/, "")
);

return toTokenAuthentication(
options.installationId,
token,
expires_at,
repositoryIds
permissions,
repositoryIds,
singleFileName
);
}
29 changes: 18 additions & 11 deletions src/to-token-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ export function toTokenAuthentication(
installationId: number,
token: string,
expiresAt: string,
repositoryIds?: number[]
permissions: { [name: string]: string },
repositoryIds?: number[],
singleFileName?: string
) {
return Object.assign({
type: "token",
token: token,
tokenType: "installation",
installationId,
expiresAt,
headers: {
authorization: `token ${token}`
return Object.assign(
{
type: "token",
token: token,
tokenType: "installation",
installationId,
permissions,
expiresAt,
headers: {
authorization: `token ${token}`
},
query: {}
},
query: {}
}, repositoryIds ? {repositoryIds} : null)
repositoryIds ? { repositoryIds } : null,
singleFileName ? { singleFileName } : null
);
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ export type StrategyOptionsWithDefaults = StrategyOptions & {
cache: Cache;
};

export type Permissions = {
[name: string]: string;
};

export type AuthOptions = {
installationId: number;
repositoryIds?: number[];
permissions?: Permissions;
url?: string;
};

0 comments on commit 8d54c03

Please sign in to comment.