|
| 1 | +import axios from 'axios' |
| 2 | +import process from 'process' |
| 3 | +import { Logger } from 'winston' |
| 4 | +export class KratosService { |
| 5 | + private KRATOS_INSTANCE: string | undefined |
| 6 | + |
| 7 | + constructor(private logger: Logger) { |
| 8 | + this.KRATOS_INSTANCE = process.env.KRATOS_ADMIN_URL |
| 9 | + if (!this.KRATOS_INSTANCE) { |
| 10 | + logger.error('No Kratos instance found.') |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + private getIdentityId = async (userEmail: string) => { |
| 15 | + try { |
| 16 | + const response = await axios.get( |
| 17 | + `${this.KRATOS_INSTANCE}/identities?credentials_identifier=${userEmail}` |
| 18 | + ) |
| 19 | + if (response.data.length > 0 && response.data[0].id) { |
| 20 | + this.logger.debug( |
| 21 | + `User with email ${userEmail} exists on the system with the ID: ${response.data[0].id}` |
| 22 | + ) |
| 23 | + return response.data[0].id |
| 24 | + } |
| 25 | + this.logger.debug(`No user with email ${userEmail} exists on the system`) |
| 26 | + return null |
| 27 | + } catch (error) { |
| 28 | + if (axios.isAxiosError(error)) { |
| 29 | + this.logger.error( |
| 30 | + `Error retrieving identity ${userEmail}:`, |
| 31 | + error.response?.status, |
| 32 | + error.response?.data |
| 33 | + ) |
| 34 | + } else { |
| 35 | + this.logger.error( |
| 36 | + `An unexpected error occurred while trying to retrieve the identity for ${userEmail}:`, |
| 37 | + error |
| 38 | + ) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private createIdentity = async (userEmail: string) => { |
| 44 | + try { |
| 45 | + const response = await axios.post( |
| 46 | + `${this.KRATOS_INSTANCE}/identities`, |
| 47 | + { |
| 48 | + schema_id: 'default', |
| 49 | + traits: { |
| 50 | + email: userEmail |
| 51 | + }, |
| 52 | + verifiable_addresses: [ |
| 53 | + { |
| 54 | + value: userEmail, |
| 55 | + verified: true, |
| 56 | + via: 'email', |
| 57 | + status: 'completed' |
| 58 | + } |
| 59 | + ], |
| 60 | + credentials: { |
| 61 | + password: { |
| 62 | + config: { |
| 63 | + password: '123456' |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + headers: { |
| 70 | + 'Content-Type': 'application/json' |
| 71 | + } |
| 72 | + } |
| 73 | + ) |
| 74 | + this.logger.debug( |
| 75 | + `Successfully created user ${userEmail} with ID ${response.data.id}` |
| 76 | + ) |
| 77 | + return response.data.id |
| 78 | + } catch (error) { |
| 79 | + if (axios.isAxiosError(error)) { |
| 80 | + this.logger.error( |
| 81 | + `Error creating identity for ${userEmail}:`, |
| 82 | + error.response?.status, |
| 83 | + error.response?.data |
| 84 | + ) |
| 85 | + } else { |
| 86 | + this.logger.error( |
| 87 | + `An unexpected error occurred while trying to create an identity for ${userEmail}:`, |
| 88 | + error |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private createRecoveryLink = async ( |
| 95 | + identityId: string, |
| 96 | + userEmail: string |
| 97 | + ) => { |
| 98 | + try { |
| 99 | + const response = await axios.post( |
| 100 | + `${this.KRATOS_INSTANCE}/recovery/link`, |
| 101 | + { |
| 102 | + expires_in: '12h', |
| 103 | + identity_id: identityId |
| 104 | + }, |
| 105 | + { |
| 106 | + headers: { |
| 107 | + 'Content-Type': 'application/json' |
| 108 | + } |
| 109 | + } |
| 110 | + ) |
| 111 | + return response.data.recovery_link |
| 112 | + } catch (error) { |
| 113 | + if (axios.isAxiosError(error)) { |
| 114 | + this.logger.error( |
| 115 | + `Error creating password for ${userEmail}:\n status:${error.response?.status}\nresponse:${error.response?.data}` |
| 116 | + ) |
| 117 | + } else { |
| 118 | + this.logger.error( |
| 119 | + `An unexpected error occurred while trying to create an account recovery link for ${userEmail}:`, |
| 120 | + error |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public run = async (userEmail: string) => { |
| 127 | + let identityId = await this.getIdentityId(userEmail) |
| 128 | + const event = identityId === null ? 'Registration' : 'Recovery' |
| 129 | + if (event === 'Registration') { |
| 130 | + identityId = await this.createIdentity(userEmail) |
| 131 | + } |
| 132 | + const recoveryLink = await this.createRecoveryLink(identityId, userEmail) |
| 133 | + this.logger.info(`${event} Link for ${userEmail}: ${recoveryLink}`) |
| 134 | + } |
| 135 | +} |
0 commit comments