import { IClusterClient, IClusterClientResponse } from "../../typings/IClusterClient"; import { IClusterClientProvider } from "../../typings/IClusterClientProvider"; import { Session } from "./Session"; import { Requisites } from '../Requisites/Requisites'; import topologyConstants from '../../topologyConstants'; export interface AuthServiceDeps { clusterClientProvider: IClusterClientProvider; apiKey: string; } export class AuthService { private _http: IClusterClient; private _apiKey: string; constructor({ clusterClientProvider, apiKey }: AuthServiceDeps) { this._http = clusterClientProvider.createClusterClient(topologyConstants.auth); this._apiKey = apiKey; } authenticateByPassword( login: string, password: string, realIp?: string ): Promise> { return this._http.send({ url: "auth/v5.6/authenticate-by-pass", method: "POST", data: password, params: { login, apiKey: this._apiKey }, headers: realIp?{ "X-Real-IP": realIp }:{} }); } authenticateByLogin( login: string, channelId:string, templateId:string, spamtemplatevariables?:any, authSid?:string, unconfirmedlogin?: string, userid?: string ): Promise> { return this._http.send({ url: "auth/v5.9/authenticate-by-email", method: "POST", params: { login, channelId, templateId, spamtemplatevariables, 'auth.sid': authSid, unconfirmedlogin, userid }, headers: { 'X-Kontur-Apikey': this._apiKey } }); } approveLogin( login: string, key:string ): Promise> { return this._http.send({ url: "auth/v5.6/approve-email", method: "POST", params: {key,email:login} }); } authenticateByPhone( phone:string ): Promise> { return this._http.send({ url: "auth/v5.9/authenticate-by-phone", method: "POST", params: { phone } }); } approvePhone( phone:string, key:string, ip?:string ): Promise> { return this._http.send({ url: "auth/v5.9/approve-phone", method: "POST", params: { phone }, data:JSON.stringify(key), }); } authenticateByCertificate( free: boolean, certificate: string ): Promise> { return this._http.send({ url: "auth/v5.11/authenticate-by-cert", method: "POST", data: certificate, params: {free} }); } approveCertificate( thumbprint: string, decryptedKey:string, ): Promise> { return this._http.send({ url: "auth/v5.11/approve-cert", method: "POST", data:decryptedKey, params: {thumbprint} }); } setPassword(id:string,password:string,sid:string): Promise> { return this._http.send({ url: "auth/v5.6/set-password", method: "POST", data: { password }, params: { id, sid, apiKey: this._apiKey } }); } getSession(sid: string): Promise> { return this._http.send({ method: "GET", url: "sessions/v5.6/sessions/" + sid }); } removeSession( sid: string ): Promise> { return this._http.send({ method: "DELETE", url: "sessions/v5.6/sessions/" + sid }); } trustedRegisterUser( user:{login:string,password?:string, userId?:string,requisites?:Requisites}, sid:string ): Promise> { return this._http.send({ url: `auth/v5.6/user-register-trusted`, method: "POST", data: user, params: { apiKey: this._apiKey, 'auth.sid':sid } }); } registerUser( user:{login:string,channelId:string,password?:string, userId?:string,requisites?:Requisites}, sid:string ): Promise> { return this._http.send({ url: `auth/v5.6/user-register`, method: "POST", data: user, params: { apiKey: this._apiKey, 'auth.sid':sid } }); } resolveApikey( apiKey: string ): Promise> { return this._http.send<{ ServiceName: string }>({ method: "POST", url: "sessions/v5.9/resolve-apikey", data: { apiKey } }); } }