1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

Implement User-based API Keys (#197)

* Added support for authenticating with an API key

* added api service methods for user api keys

* fixed a copy/pasted api endpoint url

* Let toIdentityToken() use a a prestored client_id in place of the application client_id if one exists

* Allowed for api key auth in the cli

* Removed some commented out code commited for apiKey auth

* Cleanup for ApiKey auth in the CLI

* Removed cli prefix from client_crendential auth types

* Removed ClientPrefix conditional from decoded token getters

* Update src/services/api.service.ts

Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>

* formatting

* changed command from login --apiKey to login --apikey

Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
This commit is contained in:
Addison Beck
2020-11-10 15:15:40 -05:00
committed by GitHub
parent 9aa3cbf73d
commit 79b856cb6e
7 changed files with 124 additions and 24 deletions

View File

@@ -153,6 +153,8 @@ export abstract class ApiService {
postAccountRecoverDeleteToken: (request: VerifyDeleteRecoverRequest) => Promise<any>; postAccountRecoverDeleteToken: (request: VerifyDeleteRecoverRequest) => Promise<any>;
postAccountKdf: (request: KdfRequest) => Promise<any>; postAccountKdf: (request: KdfRequest) => Promise<any>;
getEnterprisePortalSignInToken: () => Promise<string>; getEnterprisePortalSignInToken: () => Promise<string>;
postUserApiKey: (id: string, request: PasswordVerificationRequest) => Promise<ApiKeyResponse>;
postUserRotateApiKey: (id: string, request: PasswordVerificationRequest) => Promise<ApiKeyResponse>;
getFolder: (id: string) => Promise<FolderResponse>; getFolder: (id: string) => Promise<FolderResponse>;
postFolder: (request: FolderRequest) => Promise<FolderResponse>; postFolder: (request: FolderRequest) => Promise<FolderResponse>;

View File

@@ -9,21 +9,27 @@ export abstract class AuthService {
code: string; code: string;
codeVerifier: string; codeVerifier: string;
ssoRedirectUrl: string; ssoRedirectUrl: string;
clientId: string;
clientSecret: string;
twoFactorProvidersData: Map<TwoFactorProviderType, { [key: string]: string; }>; twoFactorProvidersData: Map<TwoFactorProviderType, { [key: string]: string; }>;
selectedTwoFactorProviderType: TwoFactorProviderType; selectedTwoFactorProviderType: TwoFactorProviderType;
logIn: (email: string, masterPassword: string) => Promise<AuthResult>; logIn: (email: string, masterPassword: string) => Promise<AuthResult>;
logInSso: (code: string, codeVerifier: string, redirectUrl: string) => Promise<AuthResult>; logInSso: (code: string, codeVerifier: string, redirectUrl: string) => Promise<AuthResult>;
logInApiKey: (clientId: string, clientSecret: string) => Promise<AuthResult>;
logInTwoFactor: (twoFactorProvider: TwoFactorProviderType, twoFactorToken: string, logInTwoFactor: (twoFactorProvider: TwoFactorProviderType, twoFactorToken: string,
remember?: boolean) => Promise<AuthResult>; remember?: boolean) => Promise<AuthResult>;
logInComplete: (email: string, masterPassword: string, twoFactorProvider: TwoFactorProviderType, logInComplete: (email: string, masterPassword: string, twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string, remember?: boolean) => Promise<AuthResult>; twoFactorToken: string, remember?: boolean) => Promise<AuthResult>;
logInSsoComplete: (code: string, codeVerifier: string, redirectUrl: string, logInSsoComplete: (code: string, codeVerifier: string, redirectUrl: string,
twoFactorProvider: TwoFactorProviderType, twoFactorToken: string, remember?: boolean) => Promise<AuthResult>; twoFactorProvider: TwoFactorProviderType, twoFactorToken: string, remember?: boolean) => Promise<AuthResult>;
logInApiKeyComplete: (clientId: string, clientSecret: string, twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string, remember?: boolean) => Promise<AuthResult>;
logOut: (callback: Function) => void; logOut: (callback: Function) => void;
getSupportedTwoFactorProviders: (win: Window) => any[]; getSupportedTwoFactorProviders: (win: Window) => any[];
getDefaultTwoFactorProvider: (u2fSupported: boolean) => TwoFactorProviderType; getDefaultTwoFactorProvider: (u2fSupported: boolean) => TwoFactorProviderType;
makePreloginKey: (masterPassword: string, email: string) => Promise<SymmetricCryptoKey>; makePreloginKey: (masterPassword: string, email: string) => Promise<SymmetricCryptoKey>;
authingWithApiKey: () => boolean;
authingWithSso: () => boolean; authingWithSso: () => boolean;
authingWithPassword: () => boolean; authingWithPassword: () => boolean;
} }

View File

@@ -8,7 +8,6 @@ import {
Router, Router,
} from '@angular/router'; } from '@angular/router';
import { DeviceType } from '../../enums/deviceType';
import { TwoFactorProviderType } from '../../enums/twoFactorProviderType'; import { TwoFactorProviderType } from '../../enums/twoFactorProviderType';
import { TwoFactorEmailRequest } from '../../models/request/twoFactorEmailRequest'; import { TwoFactorEmailRequest } from '../../models/request/twoFactorEmailRequest';
@@ -59,8 +58,7 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
} }
async ngOnInit() { async ngOnInit() {
if ((!this.authService.authingWithSso() && !this.authService.authingWithPassword()) || if (!this.authing || this.authService.twoFactorProvidersData == null) {
this.authService.twoFactorProvidersData == null) {
this.router.navigate([this.loginRoute]); this.router.navigate([this.loginRoute]);
return; return;
} }
@@ -75,7 +73,7 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
} }
}); });
if (this.authService.authingWithSso()) { if (this.needsLock) {
this.successRoute = 'lock'; this.successRoute = 'lock';
} }
@@ -246,4 +244,12 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
this.u2f.cleanup(); this.u2f.cleanup();
} }
} }
get authing(): boolean {
return this.authService.authingWithPassword() || this.authService.authingWithSso() || this.authService.authingWithApiKey()
}
get needsLock(): boolean {
return this.authService.authingWithSso() || this.authService.authingWithApiKey();
}
} }

View File

@@ -46,7 +46,38 @@ export class LoginCommand {
let ssoCodeVerifier: string = null; let ssoCodeVerifier: string = null;
let ssoCode: string = null; let ssoCode: string = null;
if (cmd.sso != null && this.canInteract) {
let clientId: string = null;
let clientSecret: string = null;
if (cmd.apikey != null) {
const storedClientId: string = process.env.BW_CLIENTID;
const storedClientSecret: string = process.env.BW_CLIENTSECRET;
if (storedClientId == null) {
if (this.canInteract) {
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
type: 'input',
name: 'clientId',
message: 'client_id:',
});
clientId = answer.clientId;
} else {
clientId = null;
}
} else {
clientId = storedClientId;
}
if (this.canInteract && storedClientSecret == null) {
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
type: 'input',
name: 'clientSecret',
message: 'client_secret:',
});
clientSecret = answer.clientSecret;
} else {
clientSecret = storedClientSecret;
}
} else if (cmd.sso != null && this.canInteract) {
const passwordOptions: any = { const passwordOptions: any = {
type: 'password', type: 'password',
length: 64, length: 64,
@@ -117,7 +148,10 @@ export class LoginCommand {
let response: AuthResult = null; let response: AuthResult = null;
if (twoFactorToken != null && twoFactorMethod != null) { if (twoFactorToken != null && twoFactorMethod != null) {
if (ssoCode != null && ssoCodeVerifier != null) { if (clientId != null && clientSecret != null) {
response = await this.authService.logInApiKeyComplete(clientId, clientSecret, twoFactorMethod,
twoFactorToken, false);
} else if (ssoCode != null && ssoCodeVerifier != null) {
response = await this.authService.logInSsoComplete(ssoCode, ssoCodeVerifier, this.ssoRedirectUri, response = await this.authService.logInSsoComplete(ssoCode, ssoCodeVerifier, this.ssoRedirectUri,
twoFactorMethod, twoFactorToken, false); twoFactorMethod, twoFactorToken, false);
} else { } else {
@@ -125,9 +159,10 @@ export class LoginCommand {
twoFactorToken, false); twoFactorToken, false);
} }
} else { } else {
if (ssoCode != null && ssoCodeVerifier != null) { if (clientId != null && clientSecret != null) {
response = await this.authService.logInApiKey(clientId, clientSecret);
} else if (ssoCode != null && ssoCodeVerifier != null) {
response = await this.authService.logInSso(ssoCode, ssoCodeVerifier, this.ssoRedirectUri); response = await this.authService.logInSso(ssoCode, ssoCodeVerifier, this.ssoRedirectUri);
} else { } else {
response = await this.authService.logIn(email, password); response = await this.authService.logIn(email, password);
} }

View File

@@ -8,12 +8,14 @@ export class TokenRequest {
code: string; code: string;
codeVerifier: string; codeVerifier: string;
redirectUri: string; redirectUri: string;
clientId: string;
clientSecret: string;
token: string; token: string;
provider: TwoFactorProviderType; provider: TwoFactorProviderType;
remember: boolean; remember: boolean;
device?: DeviceRequest; device?: DeviceRequest;
constructor(credentials: string[], codes: string[], provider: TwoFactorProviderType, constructor(credentials: string[], codes: string[], clientIdClientSecret: string[], provider: TwoFactorProviderType,
token: string, remember: boolean, device?: DeviceRequest) { token: string, remember: boolean, device?: DeviceRequest) {
if (credentials != null && credentials.length > 1) { if (credentials != null && credentials.length > 1) {
this.email = credentials[0]; this.email = credentials[0];
@@ -22,6 +24,9 @@ export class TokenRequest {
this.code = codes[0]; this.code = codes[0];
this.codeVerifier = codes[1]; this.codeVerifier = codes[1];
this.redirectUri = codes[2]; this.redirectUri = codes[2];
} else if (clientIdClientSecret != null && clientIdClientSecret.length > 1) {
this.clientId = clientIdClientSecret[0]
this.clientSecret = clientIdClientSecret[1]
} }
this.token = token; this.token = token;
this.provider = provider; this.provider = provider;
@@ -35,7 +40,11 @@ export class TokenRequest {
client_id: clientId, client_id: clientId,
}; };
if (this.masterPasswordHash != null && this.email != null) { if (this.clientSecret != null) {
obj.scope = 'api';
obj.grant_type = 'client_credentials';
obj.client_secret = this.clientSecret;
} else if (this.masterPasswordHash != null && this.email != null) {
obj.grant_type = 'password'; obj.grant_type = 'password';
obj.username = this.email; obj.username = this.email;
obj.password = this.masterPasswordHash; obj.password = this.masterPasswordHash;

View File

@@ -179,7 +179,7 @@ export class ApiService implements ApiServiceAbstraction {
headers.set('User-Agent', this.customUserAgent); headers.set('User-Agent', this.customUserAgent);
} }
const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', { const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', {
body: this.qsStringify(request.toIdentityToken(this.platformUtilsService.identityClientId)), body: this.qsStringify(request.toIdentityToken(request.clientId ?? this.platformUtilsService.identityClientId)),
credentials: this.getCredentials(), credentials: this.getCredentials(),
cache: 'no-store', cache: 'no-store',
headers: headers, headers: headers,
@@ -360,6 +360,16 @@ export class ApiService implements ApiServiceAbstraction {
return this.send('GET', '/accounts/sso/user-identifier', null, true, true); return this.send('GET', '/accounts/sso/user-identifier', null, true, true);
} }
async postUserApiKey(id: string, request: PasswordVerificationRequest): Promise<ApiKeyResponse> {
const r = await this.send('POST', '/accounts/api-key', request, true, true);
return new ApiKeyResponse(r);
}
async postUserRotateApiKey(id: string, request: PasswordVerificationRequest): Promise<ApiKeyResponse> {
const r = await this.send('POST', '/accounts/rotate-api-key', request, true, true);
return new ApiKeyResponse(r);
}
// Folder APIs // Folder APIs
async getFolder(id: string): Promise<FolderResponse> { async getFolder(id: string): Promise<FolderResponse> {

View File

@@ -9,7 +9,6 @@ import { KeysRequest } from '../models/request/keysRequest';
import { PreloginRequest } from '../models/request/preloginRequest'; import { PreloginRequest } from '../models/request/preloginRequest';
import { TokenRequest } from '../models/request/tokenRequest'; import { TokenRequest } from '../models/request/tokenRequest';
import { ErrorResponse } from '../models/response/errorResponse';
import { IdentityTokenResponse } from '../models/response/identityTokenResponse'; import { IdentityTokenResponse } from '../models/response/identityTokenResponse';
import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorResponse'; import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorResponse';
@@ -81,6 +80,8 @@ export class AuthService implements AuthServiceAbstraction {
code: string; code: string;
codeVerifier: string; codeVerifier: string;
ssoRedirectUrl: string; ssoRedirectUrl: string;
clientId: string;
clientSecret: string;
twoFactorProvidersData: Map<TwoFactorProviderType, { [key: string]: string; }>; twoFactorProvidersData: Map<TwoFactorProviderType, { [key: string]: string; }>;
selectedTwoFactorProviderType: TwoFactorProviderType = null; selectedTwoFactorProviderType: TwoFactorProviderType = null;
@@ -118,19 +119,27 @@ export class AuthService implements AuthServiceAbstraction {
this.selectedTwoFactorProviderType = null; this.selectedTwoFactorProviderType = null;
const key = await this.makePreloginKey(masterPassword, email); const key = await this.makePreloginKey(masterPassword, email);
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key); const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
return await this.logInHelper(email, hashedPassword, null, null, null, key, return await this.logInHelper(email, hashedPassword, null, null, null, null, null,
null, null, null); key, null, null, null);
} }
async logInSso(code: string, codeVerifier: string, redirectUrl: string): Promise<AuthResult> { async logInSso(code: string, codeVerifier: string, redirectUrl: string): Promise<AuthResult> {
this.selectedTwoFactorProviderType = null; this.selectedTwoFactorProviderType = null;
return await this.logInHelper(null, null, code, codeVerifier, redirectUrl, null, null, null, null); return await this.logInHelper(null, null, code, codeVerifier, redirectUrl, null, null,
null, null, null, null);
}
async logInApiKey(clientId: string, clientSecret: string): Promise<AuthResult> {
this.selectedTwoFactorProviderType = null;
return await this.logInHelper(null, null, null, null, null, clientId, clientSecret,
null, null, null, null);
} }
async logInTwoFactor(twoFactorProvider: TwoFactorProviderType, twoFactorToken: string, async logInTwoFactor(twoFactorProvider: TwoFactorProviderType, twoFactorToken: string,
remember?: boolean): Promise<AuthResult> { remember?: boolean): Promise<AuthResult> {
return await this.logInHelper(this.email, this.masterPasswordHash, this.code, this.codeVerifier, return await this.logInHelper(this.email, this.masterPasswordHash, this.code, this.codeVerifier,
this.ssoRedirectUrl, this.key, twoFactorProvider, twoFactorToken, remember); this.ssoRedirectUrl, this.clientId, this.clientSecret, this.key, twoFactorProvider,
twoFactorToken, remember);
} }
async logInComplete(email: string, masterPassword: string, twoFactorProvider: TwoFactorProviderType, async logInComplete(email: string, masterPassword: string, twoFactorProvider: TwoFactorProviderType,
@@ -138,14 +147,21 @@ export class AuthService implements AuthServiceAbstraction {
this.selectedTwoFactorProviderType = null; this.selectedTwoFactorProviderType = null;
const key = await this.makePreloginKey(masterPassword, email); const key = await this.makePreloginKey(masterPassword, email);
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key); const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
return await this.logInHelper(email, hashedPassword, null, null, null, key, twoFactorProvider, twoFactorToken, return await this.logInHelper(email, hashedPassword, null, null, null, null, null, key,
remember); twoFactorProvider, twoFactorToken, remember);
} }
async logInSsoComplete(code: string, codeVerifier: string, redirectUrl: string, async logInSsoComplete(code: string, codeVerifier: string, redirectUrl: string,
twoFactorProvider: TwoFactorProviderType, twoFactorToken: string, remember?: boolean): Promise<AuthResult> { twoFactorProvider: TwoFactorProviderType, twoFactorToken: string, remember?: boolean): Promise<AuthResult> {
this.selectedTwoFactorProviderType = null; this.selectedTwoFactorProviderType = null;
return await this.logInHelper(null, null, code, codeVerifier, redirectUrl, null, return await this.logInHelper(null, null, code, codeVerifier, redirectUrl, null,
null, null, twoFactorProvider, twoFactorToken, remember);
}
async logInApiKeyComplete(clientId: string, clientSecret: string, twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string, remember?: boolean): Promise<AuthResult> {
this.selectedTwoFactorProviderType = null;
return await this.logInHelper(null, null, null, null, null, clientId, clientSecret, null,
twoFactorProvider, twoFactorToken, remember); twoFactorProvider, twoFactorToken, remember);
} }
@@ -233,6 +249,10 @@ export class AuthService implements AuthServiceAbstraction {
return this.cryptoService.makeKey(masterPassword, email, kdf, kdfIterations); return this.cryptoService.makeKey(masterPassword, email, kdf, kdfIterations);
} }
authingWithApiKey(): boolean {
return this.clientId != null && this.clientSecret != null;
}
authingWithSso(): boolean { authingWithSso(): boolean {
return this.code != null && this.codeVerifier != null && this.ssoRedirectUrl != null; return this.code != null && this.codeVerifier != null && this.ssoRedirectUrl != null;
} }
@@ -242,14 +262,16 @@ export class AuthService implements AuthServiceAbstraction {
} }
private async logInHelper(email: string, hashedPassword: string, code: string, codeVerifier: string, private async logInHelper(email: string, hashedPassword: string, code: string, codeVerifier: string,
redirectUrl: string, key: SymmetricCryptoKey, twoFactorProvider?: TwoFactorProviderType, redirectUrl: string, clientId: string, clientSecret: string, key: SymmetricCryptoKey,
twoFactorToken?: string, remember?: boolean): Promise<AuthResult> { twoFactorProvider?: TwoFactorProviderType, twoFactorToken?: string, remember?: boolean): Promise<AuthResult> {
const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email); const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email);
const appId = await this.appIdService.getAppId(); const appId = await this.appIdService.getAppId();
const deviceRequest = new DeviceRequest(appId, this.platformUtilsService); const deviceRequest = new DeviceRequest(appId, this.platformUtilsService);
let emailPassword: string[] = []; let emailPassword: string[] = [];
let codeCodeVerifier: string[] = []; let codeCodeVerifier: string[] = [];
let clientIdClientSecret: string[] = [];
if (email != null && hashedPassword != null) { if (email != null && hashedPassword != null) {
emailPassword = [email, hashedPassword]; emailPassword = [email, hashedPassword];
} else { } else {
@@ -260,16 +282,22 @@ export class AuthService implements AuthServiceAbstraction {
} else { } else {
codeCodeVerifier = null; codeCodeVerifier = null;
} }
if (clientId != null && clientSecret != null) {
clientIdClientSecret = [clientId, clientSecret]
} else {
clientIdClientSecret = null;
}
let request: TokenRequest; let request: TokenRequest;
if (twoFactorToken != null && twoFactorProvider != null) { if (twoFactorToken != null && twoFactorProvider != null) {
request = new TokenRequest(emailPassword, codeCodeVerifier, twoFactorProvider, twoFactorToken, remember, request = new TokenRequest(emailPassword, codeCodeVerifier, clientIdClientSecret, twoFactorProvider,
deviceRequest); twoFactorToken, remember, deviceRequest);
} else if (storedTwoFactorToken != null) { } else if (storedTwoFactorToken != null) {
request = new TokenRequest(emailPassword, codeCodeVerifier, TwoFactorProviderType.Remember, request = new TokenRequest(emailPassword, codeCodeVerifier, clientIdClientSecret, TwoFactorProviderType.Remember,
storedTwoFactorToken, false, deviceRequest); storedTwoFactorToken, false, deviceRequest);
} else { } else {
request = new TokenRequest(emailPassword, codeCodeVerifier, null, null, false, deviceRequest); request = new TokenRequest(emailPassword, codeCodeVerifier, clientIdClientSecret, null,
null, false, deviceRequest);
} }
const response = await this.apiService.postIdentityToken(request); const response = await this.apiService.postIdentityToken(request);
@@ -286,6 +314,8 @@ export class AuthService implements AuthServiceAbstraction {
this.code = code; this.code = code;
this.codeVerifier = codeVerifier; this.codeVerifier = codeVerifier;
this.ssoRedirectUrl = redirectUrl; this.ssoRedirectUrl = redirectUrl;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.key = this.setCryptoKeys ? key : null; this.key = this.setCryptoKeys ? key : null;
this.twoFactorProvidersData = twoFactorResponse.twoFactorProviders2; this.twoFactorProvidersData = twoFactorResponse.twoFactorProviders2;
result.twoFactorProviders = twoFactorResponse.twoFactorProviders2; result.twoFactorProviders = twoFactorResponse.twoFactorProviders2;
@@ -343,6 +373,8 @@ export class AuthService implements AuthServiceAbstraction {
this.code = null; this.code = null;
this.codeVerifier = null; this.codeVerifier = null;
this.ssoRedirectUrl = null; this.ssoRedirectUrl = null;
this.clientId = null;
this.clientSecret = null;
this.twoFactorProvidersData = null; this.twoFactorProvidersData = null;
this.selectedTwoFactorProviderType = null; this.selectedTwoFactorProviderType = null;
} }