Skip to content

Commit

Permalink
feat: add more admin features and change password
Browse files Browse the repository at this point in the history
  • Loading branch information
furious-luke committed Apr 26, 2021
1 parent b65a5bf commit 6339917
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/targets/adminConfirmSignUp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Services } from "../services";
import { NotAuthorizedError } from "../errors";

interface Input {
UserPoolId: string;
Username: string;
}

export type AdminConfirmSignUpTarget = (body: Input) => Promise<object | null>;

export const AdminConfirmSignUp = ({
cognitoClient,
}: Services): AdminConfirmSignUpTarget => async (body) => {
const { UserPoolId, Username } = body || {};
const userPool = await cognitoClient.getUserPool(UserPoolId);
const user = await userPool.getUserByUsername(Username);
if (!user) {
throw new NotAuthorizedError();
}
await userPool.saveUser({
...user,
UserStatus: "CONFIRMED",
// TODO: Remove existing email_verified attribute?
Attributes: [
...(user.Attributes || []),
{
Name: "email_verified",
Value: "true",
},
],
});
// TODO: Should possibly return something?
return {};
};
16 changes: 11 additions & 5 deletions src/targets/adminCreateUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Services } from "../services";
import { User } from "../services/userPoolClient";

interface Input {
UserPoolId: string;
Expand All @@ -9,15 +10,19 @@ interface Input {
DesiredDeliveryMediums?: any;
}

export type AdminCreateUserTarget = (body: Input) => Promise<null>;
interface Output {
User: User;
}

export type AdminCreateUserTarget = (body: Input) => Promise<User | null>;

export const AdminCreateUser = ({
cognitoClient,
}: Services): AdminCreateUserTarget => async (body) => {
const { UserPoolId, Username, TemporaryPassword, UserAttributes } =
body || {};
const userPool = await cognitoClient.getUserPool(UserPoolId);
await userPool.saveUser({
const user: User = {
Username,
Password: TemporaryPassword,
Attributes: UserAttributes,
Expand All @@ -26,7 +31,8 @@ export const AdminCreateUser = ({
ConfirmationCode: undefined,
UserCreateDate: new Date().getTime(),
UserLastModifiedDate: new Date().getTime(),
});
// TODO: Anything to return?
return null;
};
await userPool.saveUser(user);
// TODO: Shuldn't return password.
return user;
};
2 changes: 2 additions & 0 deletions src/targets/adminGetUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface Input {
}

interface Output {
UserStatus: string;
UserAttributes: any;
}

Expand All @@ -22,6 +23,7 @@ export const AdminGetUser = ({
throw new NotAuthorizedError();
}
return {
UserStatus: user.UserStatus,
UserAttributes: user.Attributes,
};
};
31 changes: 31 additions & 0 deletions src/targets/adminUpdateUserAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Services } from "../services";
import { NotAuthorizedError } from "../errors";

interface Input {
UserPoolId: string;
Username: string;
UserAttributes: any;
}

interface Output {
UserAttributes: any;
}

export type AdminUpdateUserAttributesTarget = (
body: Input
) => Promise<Output | null>;

export const AdminUpdateUserAttributes = ({
cognitoClient,
}: Services): AdminUpdateUserAttributesTarget => async (body) => {
const { UserPoolId, Username } = body || {};
const userPool = await cognitoClient.getUserPool(UserPoolId);
const user = await userPool.getUserByUsername(Username);
if (!user) {
throw new NotAuthorizedError();
}
// TODO: Should save the attributes.
return {
UserAttributes: user.Attributes,
};
};
31 changes: 31 additions & 0 deletions src/targets/changePassword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import jwt from "jsonwebtoken";
import { Services } from "../services";
import { NotAuthorizedError } from "../errors";

interface Input {
AccessToken: string;
PreviousPassword: string;
ProposedPassword: string;
}

export type ChangePasswordTarget = (body: Input) => Promise<object | null>;

export const ChangePassword = ({
cognitoClient,
}: Services): ChangePasswordTarget => async (body) => {
const { AccessToken, PreviousPassword, ProposedPassword } = body || {};
const claims = jwt.decode(AccessToken) as any;
const userPool = await cognitoClient.getUserPoolForClientId(claims.client_id);
const user = await userPool.getUserByUsername(claims.username);
if (!user) {
throw new NotAuthorizedError();
}
// TODO: Should check previous password.
await userPool.saveUser({
...user,
Password: ProposedPassword,
UserLastModifiedDate: new Date().getTime(),
});
// TODO: Should possibly return something?
return {};
};
6 changes: 6 additions & 0 deletions src/targets/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConfirmForgotPassword } from "./confirmForgotPassword";
import { ConfirmSignUp } from "./confirmSignUp";
import { CreateUserPoolClient } from "./createUserPoolClient";
import { ForgotPassword } from "./forgotPassword";
import { ChangePassword } from "./changePassword";
import { InitiateAuth } from "./initiateAuth";
import { ListUsers } from "./listUsers";
import { RespondToAuthChallenge } from "./respondToAuthChallenge";
Expand All @@ -12,12 +13,15 @@ import { GetUser } from "./getUser";
import { AdminCreateUser } from "./adminCreateUser";
import { AdminGetUser } from "./adminGetUser";
import { AdminDeleteUser } from "./adminDeleteUser";
import { AdminConfirmSignUp } from "./adminConfirmSignUp";
import { AdminUpdateUserAttributes } from "./adminUpdateUserAttributes";

export const Targets = {
ConfirmForgotPassword,
ConfirmSignUp,
CreateUserPoolClient,
ForgotPassword,
ChangePassword,
InitiateAuth,
ListUsers,
RespondToAuthChallenge,
Expand All @@ -26,6 +30,8 @@ export const Targets = {
AdminCreateUser,
AdminGetUser,
AdminDeleteUser,
AdminConfirmSignUp,
AdminUpdateUserAttributes,
};

type TargetName = keyof typeof Targets;
Expand Down
4 changes: 3 additions & 1 deletion src/targets/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export const SignUp = ({
UserCreateDate: new Date().getTime(),
UserLastModifiedDate: new Date().getTime(),
UserStatus: "UNCONFIRMED",
Username: uuid.v4(),
// TODO: Why was this here?
// Username: uuid.v4(),
Username: body.Username,
};

const deliveryDetails: DeliveryDetails = {
Expand Down

0 comments on commit 6339917

Please sign in to comment.