-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: target access token strategy and session
- Loading branch information
Showing
6 changed files
with
154 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
packages/services/api/src/modules/auth/lib/target-access-token-strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import type { FastifyReply, FastifyRequest, ServiceLogger } from '@hive/service-common'; | ||
import { TokenStorage } from '../../token/providers/token-storage'; | ||
import { TokensConfig } from '../../token/providers/tokens'; | ||
import { | ||
OrganizationAccessScope, | ||
ProjectAccessScope, | ||
TargetAccessScope, | ||
} from '../providers/scopes'; | ||
import { AuthNStrategy, AuthorizationPolicyStatement, Session } from './authz'; | ||
import { transformLegacyPolicies } from './legacy-permissions'; | ||
|
||
export class TargetAccessTokenSession extends Session { | ||
public readonly organizationId: string; | ||
public readonly projectId: string; | ||
public readonly targetId: string; | ||
public readonly token: string; | ||
|
||
private policies: Array<AuthorizationPolicyStatement>; | ||
|
||
constructor(args: { | ||
organizationId: string; | ||
projectId: string; | ||
targetId: string; | ||
token: string; | ||
policies: Array<AuthorizationPolicyStatement>; | ||
}) { | ||
super(); | ||
this.organizationId = args.organizationId; | ||
this.projectId = args.projectId; | ||
this.targetId = args.targetId; | ||
this.token = args.token; | ||
this.policies = args.policies; | ||
} | ||
|
||
protected loadPolicyStatementsForOrganization( | ||
_: string, | ||
): Promise<Array<AuthorizationPolicyStatement>> | Array<AuthorizationPolicyStatement> { | ||
return this.policies; | ||
} | ||
} | ||
|
||
export class TargetAccessTokenStrategy extends AuthNStrategy<TargetAccessTokenSession> { | ||
private logger: ServiceLogger; | ||
private tokensConfig: TokensConfig; | ||
|
||
constructor(deps: { logger: ServiceLogger; tokensConfig: TokensConfig }) { | ||
super(); | ||
this.logger = deps.logger.child({ module: 'OrganizationAccessTokenStrategy' }); | ||
this.tokensConfig = deps.tokensConfig; | ||
} | ||
|
||
async parse(args: { | ||
req: FastifyRequest; | ||
reply: FastifyReply; | ||
}): Promise<TargetAccessTokenSession | null> { | ||
this.logger.debug('Attempt to resolve an API token from headers'); | ||
let accessToken: string | undefined; | ||
|
||
for (const headerName in args.req.headers) { | ||
if (headerName.toLowerCase() === 'x-api-token') { | ||
const values = args.req.headers[headerName]; | ||
const singleValue = Array.isArray(values) ? values[0] : values; | ||
|
||
if (singleValue && singleValue !== '') { | ||
this.logger.debug( | ||
'Found X-API-Token header (length=%d, token=%s)', | ||
singleValue.length, | ||
maskToken(singleValue), | ||
); | ||
accessToken = singleValue; | ||
break; | ||
} | ||
} else if (headerName.toLowerCase() === 'authorization') { | ||
const values = args.req.headers[headerName]; | ||
const singleValue = Array.isArray(values) ? values[0] : values; | ||
|
||
if (singleValue && singleValue !== '') { | ||
const bearer = singleValue.replace(/^Bearer\s+/i, ''); | ||
|
||
// Skip if bearer is missing or it's JWT generated by Auth0 (not API token) | ||
if (bearer && bearer !== '' && !bearer.includes('.')) { | ||
this.logger.debug( | ||
'Found Authorization header (length=%d, token=%s)', | ||
bearer.length, | ||
maskToken(bearer), | ||
); | ||
accessToken = bearer; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!accessToken) { | ||
this.logger.debug('No access token found'); | ||
return null; | ||
} | ||
|
||
if (accessToken.length !== 32) { | ||
this.logger.debug('Invalid access token length.'); | ||
return null; | ||
} | ||
|
||
const tokens = new TokenStorage(this.logger, this.tokensConfig, { | ||
requestId: args.req.headers['x-request-id'] as string, | ||
} as any); | ||
|
||
const result = await tokens.getToken({ token: accessToken }); | ||
|
||
return new TargetAccessTokenSession({ | ||
organizationId: result.organization, | ||
projectId: result.project, | ||
targetId: result.target, | ||
token: accessToken, | ||
policies: transformLegacyPolicies( | ||
result.organization, | ||
result.project, | ||
result.target, | ||
result.scopes as Array<OrganizationAccessScope | ProjectAccessScope | TargetAccessScope>, | ||
), | ||
}); | ||
} | ||
} | ||
|
||
function maskToken(token: string) { | ||
if (token.length > 6) { | ||
return token.substring(0, 3) + '*'.repeat(token.length - 6) + token.substring(token.length - 3); | ||
} | ||
|
||
return '*'.repeat(token.length); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
packages/services/api/src/modules/auth/providers/tokens.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters