-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK-590] wrap metadata calls for node and web (#592)
- Loading branch information
1 parent
9522b6b
commit 961fb9c
Showing
21 changed files
with
1,685 additions
and
1,195 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,182 @@ | ||
import ServiceBase from "./ServiceBase"; | ||
import { | ||
AccountClient, | ||
AccountProfile, | ||
ConfirmationMethod, | ||
InfoRequest, | ||
InfoResponse, | ||
SignInRequest, | ||
TokenProtection, | ||
ServiceOptions, | ||
ListDevicesRequest, | ||
ListDevicesResponse, | ||
RevokeDeviceRequest, | ||
RevokeDeviceResponse, | ||
ResponseStatus, | ||
AccountClient, | ||
AccountProfile, | ||
ConfirmationMethod, | ||
InfoRequest, | ||
InfoResponse, | ||
ListDevicesRequest, | ||
ListDevicesResponse, | ||
ResponseStatus, | ||
RevokeDeviceRequest, | ||
RevokeDeviceResponse, | ||
ServiceOptions, | ||
SignInRequest, | ||
TokenProtection, | ||
} from "./proto"; | ||
import {BlindOberonTokenRequest, Oberon, UnBlindOberonTokenRequest} from "@trinsic/okapi"; | ||
import { | ||
BlindOberonTokenRequest, | ||
Oberon, | ||
UnBlindOberonTokenRequest, | ||
} from "@trinsic/okapi"; | ||
|
||
export class AccountService extends ServiceBase { | ||
client: AccountClient; | ||
|
||
constructor(options?: ServiceOptions) { | ||
super(options); | ||
client: AccountClient; | ||
|
||
this.client = new AccountClient(this.address, this.channelCredentials); | ||
} | ||
constructor(options?: ServiceOptions) { | ||
super(options); | ||
|
||
public signIn(request: SignInRequest): Promise<string> { | ||
request.setEcosystemId(request.getEcosystemId() || this.options.getDefaultEcosystem()); | ||
this.client = new AccountClient(this.address, this.channelCredentials); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
this.client.signIn(request, (error, response) => { | ||
if (error || response.getStatus() != ResponseStatus.SUCCESS) { | ||
reject(error); | ||
} else { | ||
const authToken = Buffer | ||
.from(response.getProfile()!.serializeBinary()) | ||
.toString('base64url'); | ||
/** | ||
* protect the given profile | ||
* @param profile The profile to protect with oberon blinding | ||
* @param securityCode must be utf-8 encoded `UInt8Array`. `string` will be decoded to utf-8. | ||
*/ | ||
public static async protect( | ||
profile: string | AccountProfile, | ||
securityCode: string | Uint8Array | ||
): Promise<string> { | ||
securityCode = AccountService.convertToUtf8(securityCode); | ||
profile = AccountService.convertToProfile(profile); | ||
let cloned = profile.clone(); | ||
const request = new BlindOberonTokenRequest() | ||
.setToken(cloned.getAuthToken()) | ||
.setBlindingList([securityCode]); | ||
const result = await Oberon.blindToken(request); | ||
cloned = cloned | ||
.setAuthToken(result.getToken()) | ||
.setProtection( | ||
new TokenProtection() | ||
.setEnabled(true) | ||
.setMethod(ConfirmationMethod.OTHER) | ||
); | ||
return Buffer.from(cloned.serializeBinary()).toString("base64url"); | ||
} | ||
|
||
// set the auth token as active for the current service instance | ||
this.options.setAuthToken(authToken); | ||
/** | ||
* unprotect the given profile | ||
* @param profile The profile to unprotect with oberon blinding | ||
* @param securityCode must be utf-8 encoded `UInt8Array`. `string` will be decoded to utf-8. | ||
*/ | ||
public static async unprotect( | ||
profile: string | AccountProfile, | ||
securityCode: string | Uint8Array | ||
): Promise<string> { | ||
securityCode = AccountService.convertToUtf8(securityCode); | ||
profile = AccountService.convertToProfile(profile); | ||
let cloned = profile.clone(); | ||
const request = new UnBlindOberonTokenRequest() | ||
.setToken(cloned.getAuthToken()) | ||
.setBlindingList([securityCode]); | ||
const result = await Oberon.unblindToken(request); | ||
cloned = cloned | ||
.setAuthToken(result.getToken()) | ||
.setProtection( | ||
new TokenProtection() | ||
.setEnabled(false) | ||
.setMethod(ConfirmationMethod.NONE) | ||
); | ||
return Buffer.from(cloned.serializeBinary()).toString("base64url"); | ||
} | ||
|
||
resolve(authToken); | ||
} | ||
}); | ||
}); | ||
private static convertToProfile( | ||
profile: string | AccountProfile | ||
): AccountProfile { | ||
if (typeof profile == "string") { | ||
return AccountProfile.deserializeBinary( | ||
Buffer.from(profile, "base64url") | ||
); | ||
} | ||
return profile; | ||
} | ||
|
||
public info(): Promise<InfoResponse> { | ||
const request = new InfoRequest(); | ||
|
||
return new Promise(async (resolve, reject) => { | ||
try { | ||
let metadata = await this.getMetadata(request) | ||
this.client.info(request, metadata, (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(response); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
private static convertToUtf8(securityCode: string | Uint8Array): Uint8Array { | ||
if (typeof securityCode == "string") { | ||
return new TextEncoder().encode(securityCode); | ||
} else { | ||
return securityCode; | ||
} | ||
} | ||
|
||
/** | ||
* protect the given profile | ||
* @param profile The profile to protect with oberon blinding | ||
* @param securityCode must be utf-8 encoded `UInt8Array`. `string` will be decoded to utf-8. | ||
*/ | ||
public static async protect(profile: string | AccountProfile, securityCode: string | Uint8Array): Promise<string> { | ||
securityCode = AccountService.convertToUtf8(securityCode); | ||
profile = AccountService.convertToProfile(profile); | ||
let cloned = profile.clone(); | ||
const request = new BlindOberonTokenRequest().setToken(cloned.getAuthToken()).setBlindingList([securityCode]); | ||
const result = await Oberon.blindToken(request); | ||
cloned = cloned.setAuthToken(result.getToken()).setProtection(new TokenProtection().setEnabled(true).setMethod(ConfirmationMethod.OTHER)); | ||
return Buffer.from(cloned.serializeBinary()).toString('base64url'); | ||
} | ||
public signIn(request: SignInRequest): Promise<string> { | ||
request.setEcosystemId( | ||
request.getEcosystemId() || this.options.getDefaultEcosystem() | ||
); | ||
|
||
/** | ||
* unprotect the given profile | ||
* @param profile The profile to unprotect with oberon blinding | ||
* @param securityCode must be utf-8 encoded `UInt8Array`. `string` will be decoded to utf-8. | ||
*/ | ||
public static async unprotect(profile: string | AccountProfile, securityCode: string | Uint8Array): Promise<string> { | ||
securityCode = AccountService.convertToUtf8(securityCode); | ||
profile = AccountService.convertToProfile(profile); | ||
let cloned = profile.clone(); | ||
const request = new UnBlindOberonTokenRequest().setToken(cloned.getAuthToken()).setBlindingList([securityCode]); | ||
const result = await Oberon.unblindToken(request); | ||
cloned = cloned.setAuthToken(result.getToken()).setProtection(new TokenProtection().setEnabled(false).setMethod(ConfirmationMethod.NONE)); | ||
return Buffer.from(cloned.serializeBinary()).toString('base64url'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.client.signIn(request, (error, response) => { | ||
if (error || response.getStatus() != ResponseStatus.SUCCESS) { | ||
reject(error); | ||
} else { | ||
const authToken = Buffer.from( | ||
response.getProfile()!.serializeBinary() | ||
).toString("base64url"); | ||
|
||
private static convertToProfile(profile: string | AccountProfile): AccountProfile { | ||
if (typeof profile == 'string') { | ||
return AccountProfile.deserializeBinary(Buffer.from(profile, 'base64url')); | ||
} | ||
return profile; | ||
} | ||
// set the auth token as active for the current service instance | ||
this.options.setAuthToken(authToken); | ||
|
||
private static convertToUtf8(securityCode: string | Uint8Array): Uint8Array { | ||
if (typeof securityCode == 'string') { | ||
return new TextEncoder().encode(securityCode); | ||
} else { | ||
return securityCode; | ||
resolve(authToken); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
public listDevices(request: ListDevicesRequest): Promise<ListDevicesResponse> { | ||
return new Promise(async (resolve, reject) => { | ||
this.client.listDevices(request, await this.getMetadata(request), (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(response); | ||
} | ||
}) | ||
}) | ||
} | ||
public info(): Promise<InfoResponse> { | ||
const request = new InfoRequest(); | ||
|
||
public revokeDevice(request: RevokeDeviceRequest): Promise<RevokeDeviceResponse> { | ||
return new Promise(async (resolve, reject) => { | ||
this.client.revokeDevice(request, await this.getMetadata(request), (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(response); | ||
} | ||
}) | ||
}) | ||
} | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
let metadata = await this.getMetadata(request); | ||
this.client.info(request, metadata, (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(response); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
|
||
public listDevices( | ||
request: ListDevicesRequest | ||
): Promise<ListDevicesResponse> { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
let metadata = await this.getMetadata(request); | ||
this.client.listDevices(request, metadata, (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(response); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
|
||
public revokeDevice( | ||
request: RevokeDeviceRequest | ||
): Promise<RevokeDeviceResponse> { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
let metadata = await this.getMetadata(request); | ||
this.client.revokeDevice(request, metadata, (error, response) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(response); | ||
} | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.