Skip to content

Commit

Permalink
Merge pull request #10 from Akord-com/auth
Browse files Browse the repository at this point in the history
create auth module & wrap auth functions there
  • Loading branch information
wkolod authored Sep 29, 2022
2 parents 1e594f4 + d0b9f19 commit 85f3c7a
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import Akord from "@akord/akord-js";
#### Init Akord
##### with email & password
```js
const akord = Akord.signIn(email, password);
const { akord, wallet, jwtToken } = Akord.auth.signIn(email, password);
```
##### with Akord Wallet & JWT
```js
const akord = Akord.init(akordWallet, jwtToken);
const akord = Akord.init(wallet, jwtToken);
```

#### Create vault
Expand Down
9 changes: 2 additions & 7 deletions src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import Akord from "../akord";
import { AkordWallet } from "@akord/crypto";
import ApiAuthenticator from "../api/akord/api-authenticator";
import Akord from "../index";

export async function initInstance(email: string, password: string) : Promise<Akord> {
const apiAuthenticator = new ApiAuthenticator();
const jwtToken = await apiAuthenticator.getJWTToken(email, password);
const userAttributes = await apiAuthenticator.getUserAttributes(email, password);
const wallet = await AkordWallet.importFromEncBackupPhrase(password, userAttributes["custom:encBackupPhrase"]);
const { wallet, jwtToken } = await Akord.auth.signIn(email, password);
return new Akord(wallet, jwtToken, { debug: true });
}
5 changes: 3 additions & 2 deletions src/akord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { StackService } from "./service/stack";
import { NoteService } from "./service/note";
import { ProfileService } from "./service/profile";
import { Contract } from "./model/contract";
import { Auth } from "./auth";

export default class Akord {
static readonly reactionEmoji = reactionEmoji;
Expand All @@ -21,14 +22,14 @@ export default class Akord {
public memo: MemoService;
public folder: FolderService;
public membership: MembershipService;
public vault: VaultService;;
public vault: VaultService;
public stack: StackService;
public note: NoteService;
public profile: ProfileService;
public service: Service;

public static init: (wallet: Wallet, jwtToken?: string, apiConfig?: ClientConfig) => Promise<Akord>;
static signIn: (email: string, password: string) => Promise<Akord>;
public static auth: Auth;

// TODO: JWT token provider
/**
Expand Down
64 changes: 59 additions & 5 deletions src/api/akord/api-authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ export default class ApiAuthenticator {
}

public getCognitoUser(username: string): AmazonCognitoIdentity.CognitoUser {
const poolData = {
UserPoolId: this.config.aws_user_pools_id,
ClientId: this.config.aws_user_pools_web_client_id
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const userPool = this.getCognitoUserPool();
const userData = {
Username: username,
Pool: userPool
Expand All @@ -25,6 +21,15 @@ export default class ApiAuthenticator {
return cognitoUser;
}

public getCognitoUserPool(): AmazonCognitoIdentity.CognitoUserPool {
const poolData = {
UserPoolId: this.config.aws_user_pools_id,
ClientId: this.config.aws_user_pools_web_client_id
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
return userPool;
}

public async getJWTToken(username: string, password: string): Promise<string> {
const { session } = await this.authenticateUser(username, password);
return session.getAccessToken().getJwtToken();
Expand Down Expand Up @@ -76,4 +81,53 @@ export default class ApiAuthenticator {
}
);
}

public async signUp(email: string, password: string, customAttributes: any, clientMetadata?: any): Promise<Object> {
let attributes = [];

for (const [key, value] of Object.entries(customAttributes)) {
attributes.push(new AmazonCognitoIdentity.CognitoUserAttribute({
Name: key,
Value: <string>value
}));
}

const userPool = this.getCognitoUserPool();

return new Promise((resolve, reject) =>
userPool.signUp(email, password, attributes, null, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
}, clientMetadata)
);
}

public async resendCode(email: string): Promise<Object> {
const user = this.getCognitoUser(email);
return new Promise((resolve, reject) =>
user.resendConfirmationCode((err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
})
);
}

public async verifyAccount(email: string, code: string): Promise<Object> {
const user = this.getCognitoUser(email);
return new Promise((resolve, reject) =>
user.confirmRegistration(code, false, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
})
);
}
}
55 changes: 55 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Akord from "./akord";
import { AkordWallet } from "@akord/crypto";
import ApiAuthenticator from "./api/akord/api-authenticator";

class Auth {
apiAuthenticator: ApiAuthenticator;

constructor() {
this.apiAuthenticator = new ApiAuthenticator();
}

/**
* @param {string} email
* @param {string} password
* @returns Promise with Akord Client instance, JWT token & Akord Wallet
*/
public signIn = async function (email: string, password: string): Promise<{ akord: Akord, wallet: AkordWallet, jwtToken: string }> {
const jwtToken = await this.apiAuthenticator.getJWTToken(email, password);
const userAttributes = await this.apiAuthenticator.getUserAttributes(email, password);
const wallet = await AkordWallet.importFromEncBackupPhrase(password, userAttributes["custom:encBackupPhrase"]);
return { wallet, jwtToken, akord: new Akord(wallet, jwtToken) };
};

/**
* @param {string} email
* @param {string} password
* @param {any} clientMetadata JSON client metadata, ex: { clientType: "CLI" }
* @returns Promise with Akord Wallet
*/
public signUp = async function (email: string, password: string, clientMetadata: any = {}): Promise<AkordWallet> {
const wallet = await AkordWallet.create(password);
await this.apiAuthenticator.signUp(email, password, {
email,
"custom:encBackupPhrase": wallet.encBackupPhrase,
"custom:publicKey": await wallet.publicKey(),
"custom:publicSigningKey": await wallet.signingPublicKey(),
"custom:mode": "dark",
"custom:notifications": "true",
}, clientMetadata);
return wallet;
};

/**
* @param {string} email
* @param {string} code
* @returns
*/
public verifyAccount = async function (email: string, code: string): Promise<void> {
await this.apiAuthenticator.verifyAccount(email, code);
};
};

export {
Auth
}
17 changes: 3 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Akord from "./akord";
import { ClientConfig } from "./client-config";
import { Wallet, AkordWallet } from "@akord/crypto";
import ApiAuthenticator from "./api/akord/api-authenticator";
import { Wallet } from "@akord/crypto";
import { Auth } from "./auth";

/**
* @param {Wallet} wallet
Expand All @@ -13,17 +13,6 @@ Akord.init = async function (wallet: Wallet, jwtToken?: string, config: ClientCo
return new Akord(wallet, jwtToken, config);
};

/**
* @param {string} email
* @param {string} password
* @returns Promise with Akord Client instance
*/
Akord.signIn = async function (email: string, password: string): Promise<Akord> {
const apiAuthenticator = new ApiAuthenticator();
const jwtToken = await apiAuthenticator.getJWTToken(email, password);
const userAttributes = await apiAuthenticator.getUserAttributes(email, password);
const wallet = await AkordWallet.importFromEncBackupPhrase(password, userAttributes["custom:encBackupPhrase"]);
return new Akord(wallet, jwtToken);
};
Akord.auth = new Auth();

export default Akord;

0 comments on commit 85f3c7a

Please sign in to comment.