1+ const User = require ( './security/User.js' ) ;
2+
3+ /**
4+ * Auth controller
5+ *
6+ * @param kuzzle
7+ * @constructor
8+ */
9+ class Auth {
10+
11+ /**
12+ * constructor
13+ * @param kuzzle
14+ */
15+ constructor ( kuzzle ) {
16+ Object . defineProperty ( this , 'kuzzle' , {
17+ value : kuzzle
18+ } ) ;
19+ }
20+
21+ /**
22+ * Checks whether a given jwt token still represents a valid session in Kuzzle.
23+ *
24+ * @param {string } token The jwt token to check
25+ * @return {Promise|*|PromiseLike<T>|Promise<T> }
26+ */
27+ checkToken ( token ) {
28+ const
29+ request = {
30+ body : {
31+ token
32+ }
33+ } ;
34+
35+ return this . kuzzle . query ( { controller : 'auth' , action : 'checkToken' } , request , { queuable : false } )
36+ . then ( res => res . result ) ;
37+ }
38+
39+ /**
40+ * Create credentials of the specified <strategy> for the current user.
41+ *
42+ * @param credentials
43+ * @param strategy
44+ * @param options
45+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
46+ */
47+ createMyCredentials ( strategy , credentials , options ) {
48+ return this . kuzzle . query ( { controller : 'auth' , action : 'createMyCredentials' } , {
49+ strategy,
50+ body : credentials
51+ } , options )
52+ . then ( res => res . result ) ;
53+ }
54+
55+ /**
56+ * Check the existence of the specified <strategy>'s credentials for the current user.
57+ *
58+ * @param strategy
59+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
60+ */
61+ credentialsExist ( strategy , options ) {
62+ return this . kuzzle . query ( { controller : 'auth' , action : 'credentialsExist' } , { strategy} , options )
63+ . then ( res => res . result ) ;
64+ }
65+
66+ /**
67+ * Delete credentials of the specified <strategy> for the current user.
68+ *
69+ * @param strategy
70+ * @param options
71+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
72+ */
73+ deleteMyCredentials ( strategy , options ) {
74+ return this . kuzzle . query ( { controller : 'auth' , action : 'deleteMyCredentials' } , { strategy} , options )
75+ . then ( res => res . result ) ;
76+ }
77+
78+ /**
79+ * Fetches the current user.
80+ *
81+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
82+ */
83+ getCurrentUser ( ) {
84+ return this . kuzzle . query ( { controller : 'auth' , action : 'getCurrentUser' } , { } , undefined )
85+ . then ( res => {
86+ return new User ( this . kuzzle . security , res . result . _id , res . result . _source , res . result . _meta ) ;
87+ } ) ;
88+ }
89+
90+ /**
91+ * Get credential information of the specified <strategy> for the current user.
92+ *
93+ * @param strategy
94+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
95+ */
96+ getMyCredentials ( strategy , options ) {
97+ return this . kuzzle . query ( { controller : 'auth' , action : 'getMyCredentials' } , { strategy} , options )
98+ . then ( res => res . result ) ;
99+ }
100+
101+ /**
102+ * Gets the rights array of the currently logged user.
103+ *
104+ * @param {object } [options] - Optional parameters
105+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
106+ */
107+ getMyRights ( options ) {
108+ return this . kuzzle . query ( { controller : 'auth' , action : 'getMyRights' } , { } , options )
109+ . then ( res => res . result . hits ) ;
110+ }
111+
112+ /**
113+ * Get all the strategies registered in Kuzzle by all auth plugins
114+ *
115+ * @param {object } [options] - Optional parameters
116+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
117+ */
118+ getStrategies ( options ) {
119+ return this . kuzzle . query ( { controller : 'auth' , action : 'getStrategies' } , { } , options )
120+ . then ( res => res . result ) ;
121+ }
122+
123+ /**
124+ * Send login request to kuzzle with credentials
125+ * If login success, store the jwt into kuzzle object
126+ *
127+ * @param strategy
128+ * @param credentials
129+ * @param expiresIn
130+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
131+ */
132+ login ( strategy , credentials , expiresIn ) {
133+ if ( ! strategy || typeof strategy !== 'string' ) {
134+ return Promise . reject ( new Error ( 'Auth.login: strategy required' ) ) ;
135+ }
136+
137+ const
138+ request = {
139+ strategy,
140+ body : { }
141+ } ;
142+
143+ request . body = credentials || { } ;
144+ if ( expiresIn ) {
145+ request . expiresIn = expiresIn ;
146+ }
147+
148+ return this . kuzzle . query ( { controller : 'auth' , action : 'login' } , request , { queuable : false } )
149+ . then ( response => {
150+ try {
151+ this . kuzzle . setJwt ( response . result . jwt ) ;
152+ this . kuzzle . emit ( 'loginAttempt' , { success : true } ) ;
153+ } catch ( err ) {
154+ return Promise . reject ( err ) ;
155+ }
156+ return response . result . jwt ;
157+ } )
158+ . catch ( err => {
159+ this . kuzzle . emit ( 'loginAttempt' , { success : false , error : err . message } ) ;
160+ return new Error ( err ) ;
161+ } ) ;
162+ }
163+
164+ /**
165+ * Send logout request to kuzzle with jwt.
166+ *
167+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
168+ */
169+ logout ( ) {
170+ return this . kuzzle . query ( { controller : 'auth' , action : 'logout' } , { } , { queuable : false } )
171+ . then ( ( ) => this . kuzzle . unsetJwt ( ) ) ;
172+ }
173+
174+ /**
175+ * Update credentials of the specified <strategy> for the current user.
176+ *
177+ * @param strategy
178+ * @param credentals
179+ * @param options
180+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
181+ */
182+ updateMyCredentials ( strategy , credentials , options ) {
183+ return this . kuzzle . query ( { controller : 'auth' , action : 'updateMyCredentials' } , {
184+ strategy,
185+ body : credentials
186+ } , options )
187+ . then ( res => res . result ) ;
188+ }
189+
190+ /**
191+ * Update current user in Kuzzle.
192+ *
193+ * @param {object } content - a plain javascript object representing the user's modification
194+ * @param {object } [options] - (optional) arguments
195+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
196+ */
197+ updateSelf ( content , options ) {
198+ return this . kuzzle . query ( { controller : 'auth' , action : 'updateSelf' } , { body : content } , options )
199+ . then ( res => res . result ) ;
200+ }
201+
202+ /**
203+ * Validate credentials of the specified <strategy> for the current user.
204+ *
205+ * @param strategy
206+ * @param credentials
207+ * @param options
208+ * @returns {Promise|*|PromiseLike<T>|Promise<T> }
209+ */
210+ validateMyCredentials ( strategy , credentials , options ) {
211+ return this . kuzzle . query ( { controller : 'auth' , action : 'validateMyCredentials' } , {
212+ strategy,
213+ body : credentials
214+ } , options )
215+ . then ( res => res . result ) ;
216+ }
217+
218+ }
219+
220+ module . exports = Auth ;
0 commit comments