Skip to content

Commit a692849

Browse files
committed
Creating new SSO auth token for new user or tenant
Signed-off-by: ypartovski <ypartovski@vmware.com>
1 parent 2a3aeed commit a692849

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

common/src/maven/MavenCliProxy.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as path from "path"
77

88
import * as fs from "fs-extra"
9-
9+
import * as jwtDecode from "jwt-decode"
1010

1111
import { BaseEnvironment } from "../platform"
1212
import { MavenInfo } from "../types"
@@ -39,7 +39,7 @@ export class MavenCliProxy {
3939
this.writeTokenPom(tokenPom)
4040

4141
let token = fs.existsSync(tokenFile) ? this.readTokenFile(tokenFile) : null
42-
if (!token || this.isExpired(token)) {
42+
if (!token || this.isExpired(token) || this.isDiffUserOrTenant(token)) {
4343
const command = `mvn vrealize:auth -P${this.mavenSettings.profile} -DoutputDir="${tokenFolder}" -N -e`
4444
const cmdOptions = { cwd: tokenFolder }
4545

@@ -144,4 +144,39 @@ export class MavenCliProxy {
144144

145145
return now > expirationDate
146146
}
147+
148+
private isDiffUserOrTenant(token: { value: string; expirationDate: string }): boolean {
149+
let decodedToken
150+
try {
151+
decodedToken = jwtDecode(token.value)
152+
} catch (e) {
153+
this.logger.warn(`Invalid local SSO authentication token format!`)
154+
return true;
155+
}
156+
157+
// token (stored locally) details
158+
const tokenUserQualifier = decodedToken.prn // user@TENANT
159+
if (!tokenUserQualifier) {
160+
return true;
161+
}
162+
const tokenUsername = tokenUserQualifier.match(/.+?(?=@)/)
163+
if (!tokenUsername) {
164+
return true;
165+
}
166+
const tokenTenant = tokenUserQualifier.match(/(?<=@).+[^\s]/)
167+
if (!tokenTenant) {
168+
return true;
169+
}
170+
const tokenDomain = decodedToken.domain
171+
if (!tokenDomain) {
172+
return true;
173+
}
174+
175+
// Maven active profile details
176+
const vroUsername = this.environment.getVroUsername() // user@domain
177+
const vroTenant = this.environment.getVroTenant()
178+
179+
return (`${tokenUsername[0]}@${tokenDomain}`.toUpperCase() != vroUsername.toUpperCase() ||
180+
tokenTenant[0].toUpperCase() != vroTenant.toUpperCase());
181+
}
147182
}

common/src/platform/BaseEnvironment.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,20 @@ export abstract class BaseEnvironment {
117117

118118
return this.config.activeProfile.getOptional("vro.host", "")
119119
}
120+
121+
getVroUsername(): string {
122+
if (!this.config.hasActiveProfile()) {
123+
return ""
124+
}
125+
126+
return this.config.activeProfile.getOptional("vro.username", "")
127+
}
128+
129+
getVroTenant(): string {
130+
if (!this.config.hasActiveProfile()) {
131+
return ""
132+
}
133+
134+
return this.config.activeProfile.getOptional("vro.tenant", "")
135+
}
120136
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@
579579
"fs-extra": "~7.0.0",
580580
"glob": "^7.1.6",
581581
"jsonc-parser": "^2.1.0",
582+
"jwt-decode": "^2.2.0",
582583
"lodash": "^4.17.15",
583584
"micromatch": "^4.0.2",
584585
"module-alias": "^2.2.2",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4141,6 +4141,11 @@ just-debounce@^1.0.0:
41414141
resolved "https://registry.yarnpkg.com/just-debounce/-/just-debounce-1.0.0.tgz#87fccfaeffc0b68cd19d55f6722943f929ea35ea"
41424142
integrity sha1-h/zPrv/AtozRnVX2cilD+SnqNeo=
41434143

4144+
jwt-decode@^2.2.0:
4145+
version "2.2.0"
4146+
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-2.2.0.tgz#7d86bd56679f58ce6a84704a657dd392bba81a79"
4147+
integrity sha1-fYa9VmefWM5qhHBKZX3TkruoGnk=
4148+
41444149
keytar@*:
41454150
version "6.0.1"
41464151
resolved "https://registry.yarnpkg.com/keytar/-/keytar-6.0.1.tgz#996961abdebf300b2d34bb2eab6e42a8096b1ed8"

0 commit comments

Comments
 (0)