@lunoxjs/auth
Version:
Lunox Authentication
2 lines (1 loc) • 6.89 kB
JavaScript
var m=Object.defineProperty;var s=(r,e)=>m(r,"name",{value:e,configurable:!0});import{useMagic as g,Str as v,ServiceProvider as p,Request as f}from"@lunoxjs/core";var b=Object.defineProperty,R=s((r,e)=>b(r,"name",{value:e,configurable:!0}),"r$1");const w=R(r=>class extends r{static rememberTokenName="remember_token";password;getAuthPassword(){return this.password}getAuthIdentifierName(){return this.constructor.primaryKey}getAuthIdentifier(){const e=this.getAuthIdentifierName()||"id";return this[e]}getRememberToken(){if(this.getRememberTokenName())return this[this.getRememberTokenName()]}setRememberToken(e){this.getRememberTokenName()&&(this[this.getRememberTokenName()]=e)}getRememberTokenName(){return this.constructor.rememberTokenName}},"Authenticatable");var y=Object.defineProperty,k=s((r,e)=>y(r,"name",{value:e,configurable:!0}),"e$2");let h=s(class extends Error{_guards;_redirectTo;constructor(e="Unauthenticated.",t=[],i){super(e),this._guards=t,this._redirectTo=i}guards(){return this._guards}redirectTo(){return this._redirectTo}},"s");k(h,"AuthenticationException");var A=Object.defineProperty,_=s((r,e)=>A(r,"name",{value:e,configurable:!0}),"o$1");class c{static symbol=Symbol("AuthManager");static drivers={};static userProviders={};app;guards={};request;constructor(e){this.app=e}static registerUserProvider(e,t){this.userProviders[e]=t}static registerDriver(e,t){this.drivers[e]=t}setRequest(e){return this.request=e,this}getRequest(){return this.request}guard(e){return e=e||this.getDefaultDriver(),this.guards[e]||(this.guards[e]=this.resolve(e))}getDefaultDriver(){return config("auth.defaults.guard")}resolve(e){const t=this.getConfig(e);if(!t)throw new Error(`"Auth guard [${e}] is not defined."`);return this.constructor.createDriver(e,t)}static createDriver(e,t){const i=this.drivers[t.driver];if(!i)throw new Error(`Authentication driver [${t.driver}] for guard [${e}] is not defined.`);return i(e,t)}static createUserProvider(e){const t=this.getProviderConfiguration(e);if(!t)throw new Error("cannot get user provider config");const i=t.driver,n=this.userProviders[i];if(!n)throw new Error(`Authentication user provider [${t.driver}] is not defined.`);return n(t)}getConfig(e){return config(`auth.guards.${e}`)}static getProviderConfiguration(e){if(e=e||this.getDefaultUserProvider(),e)return config("auth.providers."+e)}static getDefaultUserProvider(){return config("auth.defaults.provider")}__get(e){return(...t)=>this.guard()[e].call(this.guard(),...t)}}s(c,"AuthManager"),_(c,"AuthManager");var a=g(c),P=Object.defineProperty,T=s((r,e)=>P(r,"name",{value:e,configurable:!0}),"r");let l=s(class{_user;provider;async user(){}authenticate(){if(this._user)return this._user;throw new h}async check(){return!!await this.user()}async guest(){return!await this.check()}async id(){const e=await this.user();if(e)return e.getAuthIdentifier()}getProvider(){return this.provider}setProvider(e){this.provider=e}},"i");T(l,"GuardHelper");var $=Object.defineProperty,q=s((r,e)=>$(r,"name",{value:e,configurable:!0}),"e");class o{recaller;constructor(e){this.recaller=e}valid(){return this.properString()&&this.hasAllSegments()}properString(){return typeof this.recaller=="string"&&this.recaller.includes("|")}hasAllSegments(){const e=this.recaller.split("|");return e.length==3&&e[0].trim()!==""&&e[1].trim()!==""}id(){return this.recaller.split("|",3)[0]}token(){return this.recaller.split("|",3)[1]}}s(o,"l"),q(o,"Recaller");var D=Object.defineProperty,S=s((r,e)=>D(r,"name",{value:e,configurable:!0}),"i$1");class u extends l{name;session;request;lastAttempted;rememberDuration=2628e3;loggedOut=!1;recallAttempted=!1;viaRemember=!1;constructor(e,t,i){super(),this.name=e,this.provider=t,this.request=i,this.session=this.request.session()}async validate(e){return this.lastAttempted=await this.provider.retrieveByCredentials(e),this.hasValidCredentials(this.lastAttempted,e)}async once(e){return await this.validate(e)?(this.setUser(this.lastAttempted),!0):!1}async attempt(e={password:""},t=!1){const i=await this.provider.retrieveByCredentials(e);return this.hasValidCredentials(i,e)?(await this.login(i,t),!0):!1}hasValidCredentials(e,t){return e!=null&&this.provider.validateCredentials(e,t)}async login(e,t){await this.updateSession(e.getAuthIdentifier()),t&&(await this.ensureRememberTokenIsSet(e),this.queueRecallerCookie(e)),this.setUser(e)}queueRecallerCookie(e){this.request.cookieJar.queue(this.createRecaller(`${e.getAuthIdentifier()}|${e.getRememberToken()}|${e.getAuthPassword()}`))}createRecaller(e){return this.request.cookieJar.make(this.getRecallerName(),e,this.getRememberDuration())}getRememberDuration(){return this.rememberDuration}setRememberDuration(e){return this.rememberDuration=e,this}async ensureRememberTokenIsSet(e){e.getRememberToken()||await this.cycleRememberToken(e)}async cycleRememberToken(e){const t=v.random(60);e.setRememberToken(t),await this.provider.updateRememberToken(e,t)}async logout(){const e=await this.user();this.clearUserDataFromStorage(),this._user&&e?.getRememberToken()&&await this.cycleRememberToken(e),this._user=void 0,this.loggedOut=!0}setUser(e){return this._user=e,this.loggedOut=!1,this}getName(){return`login_${this.name}_${sha1(this.constructor.name)}`}async updateSession(e){await this.session.migrate(!0),this.session.put(this.getName(),e)}async user(){if(this.loggedOut)return;if(this._user)return this._user;const e=this.session.get(this.getName());e&&(this._user=await this.provider.retrieveById(e));const t=this.recaller();return!this._user&&t&&(this._user=await this.userFromRecaller(t),this._user&&await this.updateSession(this._user.getAuthIdentifier())),this._user}async userFromRecaller(e){if(!e.valid()||this.recallAttempted)return;this.recallAttempted=!0;const t=await this.provider.retrieveByToken(e.id(),e.token());return this.viaRemember=!!t,t}recaller(){if(!this.request)return;const e=this.request.cookies.get(this.getRecallerName());if(e)return new o(e)}getRecallerName(){return`remember_${this.name}_${sha1(this.constructor.name)}`}clearUserDataFromStorage(){this.session.remove(this.getName()),this.recaller()&&this.request.cookieJar.queue(this.request.cookieJar.forget(this.getRecallerName()))}}s(u,"a"),S(u,"SessionGuard");var N=Object.defineProperty,C=s((r,e)=>N(r,"name",{value:e,configurable:!0}),"o");class d extends p{async register(){this.app.singleton(a.symbol,()=>new a(this.app))}async boot(){a.registerDriver("session",function(e,t){const i=a.createUserProvider(t.provider),n=new u(e,i,request());return t.remember&&n.setRememberDuration(t.remember),n}),f.macro("auth",function(){return this.managers.auth?this.managers.auth:this.managers.auth=new a(this.app).setRequest(this)})}}s(d,"n"),C(d,"AuthServiceProvider");export{a as AuthManager,d as AuthServiceProvider,w as Authenticatable,h as AuthenticationException,l as GuardHelper,o as Recaller,u as SessionGuard};