|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Request } from 'express'; |
| 18 | + |
| 19 | +import { Action, Step } from '../../actions'; |
| 20 | +import { getProviderForHost } from './tokenIdentity'; |
| 21 | +import { findUserByGitAccount } from '../../../db'; |
| 22 | +import { getErrorMessage } from '../../../utils/errors'; |
| 23 | + |
| 24 | +async function exec(req: Request, action: Action): Promise<Action> { |
| 25 | + const step = new Step('resolveUserFromToken'); |
| 26 | + |
| 27 | + if (req.user) { |
| 28 | + step.log(`User already resolved via session auth: ${action.user}`); |
| 29 | + action.addStep(step); |
| 30 | + return action; |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + const authHeader = req.headers?.authorization; |
| 35 | + if (!authHeader) { |
| 36 | + step.log('No Authorization header — cannot resolve push identity from token'); |
| 37 | + action.addStep(step); |
| 38 | + return action; |
| 39 | + } |
| 40 | + |
| 41 | + const [scheme, encoded] = authHeader.split(' '); |
| 42 | + if (!scheme || !encoded || scheme.toLowerCase() !== 'basic') { |
| 43 | + step.log('Authorization header is not Basic — cannot resolve push identity'); |
| 44 | + action.addStep(step); |
| 45 | + return action; |
| 46 | + } |
| 47 | + |
| 48 | + const credentials = Buffer.from(encoded, 'base64').toString(); |
| 49 | + const separatorIndex = credentials.indexOf(':'); |
| 50 | + if (separatorIndex === -1) { |
| 51 | + step.log('Malformed Basic auth credentials'); |
| 52 | + action.addStep(step); |
| 53 | + return action; |
| 54 | + } |
| 55 | + |
| 56 | + const token = credentials.slice(separatorIndex + 1); |
| 57 | + |
| 58 | + let hostname: string; |
| 59 | + try { |
| 60 | + hostname = new URL(action.url).hostname; |
| 61 | + } catch { |
| 62 | + step.log(`Cannot parse hostname from action URL: ${action.url}`); |
| 63 | + action.addStep(step); |
| 64 | + return action; |
| 65 | + } |
| 66 | + |
| 67 | + const provider = getProviderForHost(hostname); |
| 68 | + if (!provider) { |
| 69 | + step.log(`No token identity provider for host '${hostname}' — identity resolution skipped`); |
| 70 | + action.addStep(step); |
| 71 | + return action; |
| 72 | + } |
| 73 | + |
| 74 | + const identity = await provider.fetchScmIdentity(token); |
| 75 | + if (!identity) { |
| 76 | + step.log( |
| 77 | + `${provider.name}: failed to resolve identity from token (invalid token or missing scope?)`, |
| 78 | + ); |
| 79 | + action.addStep(step); |
| 80 | + return action; |
| 81 | + } |
| 82 | + |
| 83 | + step.log( |
| 84 | + `${provider.name}: resolved SCM identity from token: ${identity.login} (${identity.email ?? 'no public email'})`, |
| 85 | + ); |
| 86 | + |
| 87 | + const user = await findUserByGitAccount(identity.login); |
| 88 | + if (user) { |
| 89 | + step.log( |
| 90 | + `Mapped SCM identity '${identity.login}' to git-proxy user '${user.username}' (${user.email})`, |
| 91 | + ); |
| 92 | + action.user = user.username; |
| 93 | + action.userEmail = user.email; |
| 94 | + } else { |
| 95 | + step.log( |
| 96 | + `No git-proxy user has gitAccount '${identity.login}' — ` + |
| 97 | + `falling back to SCM identity. ` + |
| 98 | + `Users can associate their account via PUT /api/v1/user/:username/git-account`, |
| 99 | + ); |
| 100 | + action.user = identity.login; |
| 101 | + if (identity.email) { |
| 102 | + action.userEmail = identity.email; |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (error: unknown) { |
| 106 | + const msg = getErrorMessage(error); |
| 107 | + step.log(`Failed to resolve push identity from token: ${msg}`); |
| 108 | + } |
| 109 | + |
| 110 | + action.addStep(step); |
| 111 | + return action; |
| 112 | +} |
| 113 | + |
| 114 | +exec.displayName = 'resolveUserFromToken.exec'; |
| 115 | + |
| 116 | +export { exec }; |
0 commit comments