Skip to content

Commit

Permalink
feat: allow different ssi-hub and siwe uri
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Mar 3, 2023
1 parent 00f34d2 commit 265d909
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src/modules/cache-client/auth.ts
Expand Up @@ -2,7 +2,7 @@ import { AxiosInstance, AxiosResponse } from 'axios';
import { SiweMessage } from 'siwe';
import { cacheConfigs } from '../../config';
import { SignerService } from '../signer';
import { AuthTokens, SiweOptions } from './cache-client.types';
import { AuthTokens } from './cache-client.types';

/**
* Provides authentication methods to ssi-hub
Expand All @@ -11,13 +11,25 @@ import { AuthTokens, SiweOptions } from './cache-client.types';
* @todo Make this class top-level module in order to use in application
*/
export class SsiAuth {
private readonly config: SiweOptions;
private readonly config: Partial<SiweMessage>;

constructor(
private signerService: SignerService,
private http: AxiosInstance
) {
this.config = cacheConfigs()[this.signerService.chainId].auth;
const { uri, domain } = cacheConfigs()[this.signerService.chainId].auth;
this.config = {
domain: new URL(domain).host,
uri:
uri ||
new URL(
'/v1/login/siwe/verify',
new URL(cacheConfigs()[this.signerService.chainId].url).origin
).href,
address: this.signerService.address,
version: '1',
chainId: this.signerService.chainId,
};
}

/**
Expand All @@ -29,17 +41,10 @@ export class SsiAuth {
const {
data: { nonce },
} = await this.http.post<{ nonce: string }>('/login/siwe/initiate');
const uri = new URL(
'/v1/login/siwe/verify',
new URL(cacheConfigs()[this.signerService.chainId].url).origin
).href;

const siweMessage = new SiweMessage({
...this.config,
nonce,
domain: new URL(this.config.domain).host,
address: this.signerService.address,
uri,
version: '1',
chainId: this.signerService.chainId,
});
const message = siweMessage.prepareMessage();

Expand Down
3 changes: 1 addition & 2 deletions src/modules/cache-client/cache-client.types.ts
@@ -1,12 +1,11 @@
import { SiweMessage } from 'siwe';
import { AssetHistoryEventType } from '../assets';

export enum Order {
'ASC' = 'ASC',
'DESC' = 'DESC',
}

export type SiweOptions = Pick<SiweMessage, 'domain'>;
export type SiweOptions = { domain: string; uri?: string };

export interface CacheServerClientOptions {
url: string;
Expand Down

0 comments on commit 265d909

Please sign in to comment.