|
| 1 | +import { get, set } from './cache' |
| 2 | +import { getAppAuthentication } from './get-app-authentication' |
| 3 | +import { toTokenAuthentication } from './to-token-authentication' |
| 4 | +import { |
| 5 | + InstallationAuthOptions, |
| 6 | + InstallationAccessTokenAuthentication, |
| 7 | + RequestInterface, |
| 8 | + State, |
| 9 | +} from './types' |
| 10 | + |
| 11 | +export async function getInstallationAuthentication( |
| 12 | + state: State, |
| 13 | + options: InstallationAuthOptions, |
| 14 | + customRequest?: RequestInterface |
| 15 | +): Promise<InstallationAccessTokenAuthentication> { |
| 16 | + const installationId = Number(options.installationId || state.installationId) |
| 17 | + |
| 18 | + if (!installationId) { |
| 19 | + throw new Error( |
| 20 | + '[@octokit/auth-app] installationId option is required for installation authentication.' |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + if (options.factory) { |
| 25 | + const { type, factory, ...factoryAuthOptions } = options |
| 26 | + // @ts-ignore if `options.factory` is set, the return type for `auth()` should be `Promise<ReturnType<options.factory>>` |
| 27 | + return factory(Object.assign({}, state, factoryAuthOptions)) |
| 28 | + } |
| 29 | + |
| 30 | + const optionsWithInstallationTokenFromState = Object.assign( |
| 31 | + { installationId }, |
| 32 | + options |
| 33 | + ) |
| 34 | + |
| 35 | + if (!options.refresh) { |
| 36 | + const result = await get(state.cache, optionsWithInstallationTokenFromState) |
| 37 | + if (result) { |
| 38 | + const { |
| 39 | + token, |
| 40 | + createdAt, |
| 41 | + expiresAt, |
| 42 | + permissions, |
| 43 | + repositoryIds, |
| 44 | + singleFileName, |
| 45 | + repositorySelection, |
| 46 | + } = result |
| 47 | + |
| 48 | + return toTokenAuthentication({ |
| 49 | + installationId, |
| 50 | + token, |
| 51 | + createdAt, |
| 52 | + expiresAt, |
| 53 | + permissions, |
| 54 | + repositorySelection, |
| 55 | + repositoryIds, |
| 56 | + singleFileName, |
| 57 | + }) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const appAuthentication = await getAppAuthentication(state) |
| 62 | + const request = customRequest || state.request |
| 63 | + |
| 64 | + const { |
| 65 | + data: { |
| 66 | + token, |
| 67 | + expires_at: expiresAt, |
| 68 | + repositories, |
| 69 | + permissions, |
| 70 | + // @ts-ignore |
| 71 | + repository_selection: repositorySelection, |
| 72 | + // @ts-ignore |
| 73 | + single_file: singleFileName, |
| 74 | + }, |
| 75 | + } = await request('POST /app/installations/{installation_id}/access_tokens', { |
| 76 | + installation_id: installationId, |
| 77 | + repository_ids: options.repositoryIds, |
| 78 | + permissions: options.permissions, |
| 79 | + mediaType: { |
| 80 | + previews: ['machine-man'], |
| 81 | + }, |
| 82 | + headers: { |
| 83 | + authorization: `bearer ${appAuthentication.token}`, |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + const repositoryIds = repositories |
| 88 | + ? repositories.map((r: { id: number }) => r.id) |
| 89 | + : void 0 |
| 90 | + |
| 91 | + const createdAt = new Date().toISOString() |
| 92 | + await set(state.cache, optionsWithInstallationTokenFromState, { |
| 93 | + token, |
| 94 | + createdAt, |
| 95 | + expiresAt, |
| 96 | + repositorySelection, |
| 97 | + permissions, |
| 98 | + repositoryIds, |
| 99 | + singleFileName, |
| 100 | + }) |
| 101 | + |
| 102 | + return toTokenAuthentication({ |
| 103 | + installationId, |
| 104 | + token, |
| 105 | + createdAt, |
| 106 | + expiresAt, |
| 107 | + repositorySelection, |
| 108 | + permissions, |
| 109 | + repositoryIds, |
| 110 | + singleFileName, |
| 111 | + }) |
| 112 | +} |
0 commit comments