Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.

Commit 61fa029

Browse files
author
Leonardo Chaia
committed
refactor: improvements on registry auth. Removed auth cache
1 parent d91282a commit 61fa029

File tree

4 files changed

+20
-72
lines changed

4 files changed

+20
-72
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
"@commitlint/config-conventional": "^7.0.1",
6161
"@types/dockerode": "^2.5.5",
6262
"@types/electron-settings": "^3.1.1",
63-
"@types/node": "^10.10.1",
6463
"@types/filesize": "^3.6.0",
64+
"@types/node": "^10.10.1",
6565
"@types/uuid": "^3.4.4",
6666
"angular-pipes": "^8.1.1",
6767
"angular2-hotkeys": "^2.1.3",
@@ -75,8 +75,8 @@
7575
"electron-builder": "^20.28.4",
7676
"electron-reload": "^1.2.5",
7777
"fast-text-encoding": "^1.0.0",
78-
"git-rev-sync": "^1.12.0",
7978
"filesize": "^3.6.1",
79+
"git-rev-sync": "^1.12.0",
8080
"hammerjs": "^2.0.8",
8181
"husky": "^0.14.3",
8282
"npm-run-all": "^4.1.3",
@@ -89,6 +89,7 @@
8989
"uuid": "^3.3.2",
9090
"wait-on": "3.0.1",
9191
"webdriver-manager": "^12.1.0",
92+
"www-authenticate": "^0.6.2",
9293
"xterm": "^3.7.0",
9394
"zone.js": "0.8.26"
9495
}
Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
import { Injectable } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
33
import { map, } from 'rxjs/operators';
4-
import { of } from 'rxjs';
4+
import parsers from 'www-authenticate/lib/parsers';
55

66
@Injectable({
77
providedIn: 'root'
88
})
99
export class RegistryAuthService {
1010

11-
private tokenCache: { [key: string]: string } = {};
12-
1311
constructor(private httpClient: HttpClient) { }
1412

1513
public getAccesstokenFromHeader(header: string, registryUrl: string, username: string, password: string) {
16-
const parsed = this.parseWwwAuthenticateHeader(header);
17-
const clientId = 'ui-test';
18-
19-
const cacheKey = this.getCacheKey(registryUrl, parsed.service, parsed.scope, clientId, username, password);
20-
if (this.isCached(cacheKey)) {
21-
return of(this.tokenCache[cacheKey]);
22-
}
23-
24-
return this.getAccesstoken(parsed.realm, parsed.service, parsed.scope, clientId, username, password)
25-
.pipe(map(token => {
26-
return this.tokenCache[cacheKey] = token;
27-
}));
28-
}
29-
30-
public getAccessTokenForUrl(registryUrl: string) {
31-
const key = Object.keys(this.tokenCache).filter(k => k.includes(registryUrl))[0];
32-
if (key) {
33-
return this.tokenCache[key];
34-
} else {
35-
return null;
36-
}
14+
const parsed = new parsers.WWW_Authenticate(header).parms;
15+
const clientId = 'Timoneer';
16+
return this.getAccesstoken(parsed.realm, parsed.service, parsed.scope, clientId, username, password);
3717
}
3818

3919
protected getAccesstoken(
@@ -57,34 +37,4 @@ export class RegistryAuthService {
5737
return r.token;
5838
}));
5939
}
60-
61-
protected parseWwwAuthenticateHeader(header: string) {
62-
const parts = header.split(',');
63-
const realm = parts.filter(p => p.indexOf('Bearer realm=') >= 0)[0];
64-
const service = parts.filter(p => p.indexOf('service=') >= 0)[0];
65-
const scope = parts.filter(p => p.indexOf('scope=') >= 0)[0];
66-
return {
67-
realm: realm.replace('Bearer realm="', '').replace('"', ''),
68-
service: service.replace('service="', '').replace('"', ''),
69-
scope: scope.replace('scope="', '').replace('"', ''),
70-
};
71-
}
72-
73-
protected getFromCache(key: string) {
74-
return this.tokenCache[key];
75-
}
76-
77-
protected isCached(key) {
78-
return !!this.tokenCache[key];
79-
}
80-
81-
protected getCacheKey(url: string,
82-
service: string,
83-
scope: string,
84-
clientId: string,
85-
username: string,
86-
password: string) {
87-
88-
return `${url}-${btoa(service + scope + clientId + username + password)}`;
89-
}
9040
}

src/app/registry/registry.service.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@ export class RegistryService {
4646
}
4747

4848
protected get<T>(registryUrl: string, path: string, params?: { [param: string]: string | string[]; }): Observable<T> {
49-
let attempts = 0;
50-
const recursion = (headers?: any) => {
51-
attempts++;
52-
if (attempts > 2) {
53-
console.error('stop');
54-
return;
55-
}
56-
return this.httpClient.get<T>(this.ensureEndingSlash(registryUrl) + path, {
57-
headers: headers,
58-
params: params
59-
}).pipe(catchError(e => {
49+
50+
const doHttpCall = (headers?: any) => this.httpClient.get<T>(this.ensureEndingSlash(registryUrl) + path, {
51+
headers: headers,
52+
params: params
53+
});
54+
55+
return doHttpCall()
56+
.pipe(catchError(e => {
6057
if (e.status === 401) {
6158
const header = e.headers.get('www-authenticate');
6259

@@ -74,7 +71,7 @@ export class RegistryService {
7471
return this.authService.getAccesstokenFromHeader(header, registrySettings.url,
7572
registrySettings.username, registrySettings.password)
7673
.pipe(
77-
switchMap(token => recursion({
74+
switchMap(token => doHttpCall({
7875
Authorization: `Bearer ${token}`
7976
}))
8077
);
@@ -85,10 +82,6 @@ export class RegistryService {
8582
return throwError(e);
8683
}
8784
}));
88-
};
89-
90-
const possibleToken = this.authService.getAccessTokenForUrl(registryUrl);
91-
return recursion(possibleToken ? { Authorization: `Bearer ${possibleToken}` } : null);
9285
}
9386

9487
private ensureEndingSlash(str: string) {

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8207,6 +8207,10 @@ write-file-atomic@~2.1.0:
82078207
imurmurhash "^0.1.4"
82088208
slide "^1.1.5"
82098209

8210+
www-authenticate@^0.6.2:
8211+
version "0.6.2"
8212+
resolved "https://registry.yarnpkg.com/www-authenticate/-/www-authenticate-0.6.2.tgz#b7a7d487bdc5a8b423a4d8fd5f9c661adde50ebf"
8213+
82108214
xdg-basedir@^3.0.0:
82118215
version "3.0.0"
82128216
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"

0 commit comments

Comments
 (0)