@@ -16,107 +16,127 @@ export default class MysqlAuth implements IPluginAuth<MysqlAuthConfig> {
1616
1717 private config : mysql . ConnectionConfig ;
1818 private queries : IMysqlQueries ;
19-
2019 private logger : Logger ;
21- private connOK : boolean ;
20+
21+ private authQueryEmptyWarned : boolean ;
22+ private addQueryEmptyWarned : boolean ;
23+ private passwordQueryEmptyWarned : boolean ;
2224
2325 constructor ( configuration : MysqlAuthConfig , stuff : { logger : Logger } ) {
2426
2527 this . config = configuration . connection ;
2628 this . queries = new MysqlQueries ( configuration . queries ) ;
2729 this . logger = stuff . logger ;
2830
29- this . connOK = false ;
31+ this . authQueryEmptyWarned = false ;
32+ this . addQueryEmptyWarned = false ;
33+ this . passwordQueryEmptyWarned = false ;
3034
31- // Calling initialization at the constructor check connection to the database.
32- this . test ( )
33- . then ( ( success : boolean ) => {
34- if ( success ) {
35- this . connOK = true ;
36- }
37- } )
38- . catch ( ( reason ) => {
39- this . connOK = false ;
40- } ) ;
35+ this . test ( ) ;
4136 }
4237
4338 authenticate ( user : string , password : string , cb : Callback ) {
44- const connection = mysql . createConnection ( this . config ) ;
45-
46- if ( this . queries . auth_user . length == 0 ) {
47- this . logger . info ( 'MySQL - Can\'t authenticate: authenticate query is empty' ) ;
48- cb ( null , false )
49- return ;
39+ if ( this . queries . auth_user . length == 0 && ! this . authQueryEmptyWarned ) {
40+ this . logger . warn ( 'MySQL - Can\'t authenticate: authenticate query is empty' ) ;
41+ this . authQueryEmptyWarned = true ;
42+ return cb ( null , false ) ;
5043 }
5144
52- connection . query ( this . queries . auth_user , [ user , password ] , ( error , result ) => {
53- if ( error || result . length !== 1 || result [ 0 ] . usergroups === null ) {
54- cb ( null , false ) ;
45+ this . runQuery ( this . queries . auth_user , [ user , password ] , ( error , result ) => {
46+ if ( error ) {
47+ return cb ( null , false ) ;
48+ }
49+ else if ( ! Array . isArray ( result ) ) {
50+ this . logger . error ( { } , 'MySQL - Result is not an rowset' ) ;
51+ return cb ( null , false ) ;
52+ }
53+ else if ( result [ 0 ] === null ) {
54+ this . logger . error ( { } , 'MySQL - The query returned an invalid result' ) ;
55+ return cb ( null , false ) ;
56+ }
57+ else if ( result . length === 0 ) {
58+ // Usual case where everything is valid but we got a bad password.
59+ return cb ( null , false ) ;
60+ }
61+ else if ( result . length > 1 ) {
62+ this . logger . error ( { "rows" : result . length } , "MySQL - The query returned @{rows} rows. Only one row must be returned." ) ;
63+ return cb ( null , false ) ;
5564 }
5665 else {
57- cb ( null , result [ 0 ] . usergroups . split ( ',' ) ) ;
66+ let groups : string = "" ;
67+ if ( result [ 0 ] . usergroups === undefined ) {
68+ this . logger . warn ( { } , "MySQL - The query didn't contain a `usergroups` column. Group feature will likely not be functionnal." ) ;
69+ }
70+ else if ( result [ 0 ] . usergroups !== null ) {
71+ groups = result [ 0 ] . usergroups ;
72+ }
73+
74+ return cb ( null , groups . split ( ',' ) ) ;
5875 }
59-
60- connection . end ( ) ;
6176 } ) ;
6277 }
6378
6479 adduser ( user : string , password : string , cb : Callback ) {
65- const connection = mysql . createConnection ( this . config ) ;
6680
67- if ( this . queries . add_user . length == 0 ) {
68- this . logger . info ( 'MySQL - Can\'t add user: add_user query is empty' ) ;
69- cb ( null , false )
70- return ;
81+ if ( this . queries . add_user . length == 0 && ! this . addQueryEmptyWarned ) {
82+ this . logger . warn ( { } , 'MySQL - Can\'t add user: add_user query is empty' ) ;
83+ this . addQueryEmptyWarned = true ;
84+ return cb ( null , false ) ;
7185 }
7286
73- connection . query ( this . queries . add_user , [ user , password ] , ( error , result ) => {
87+ this . runQuery ( this . queries . add_user , [ user , password ] , ( error , result ) => {
7488 if ( error ) {
75- cb ( null , false ) ;
89+ this . logger . error ( { error} , "MySQL - Error: @{error.message}" ) ;
90+ return cb ( null , false ) ;
7691 }
7792 else {
78- cb ( null , result [ 0 ] . usergroups . split ( ',' ) ) ;
93+ return cb ( null , true ) ;
7994 }
80-
81- connection . end ( ) ;
8295 } ) ;
8396 }
8497
8598 changePassword ( user : string , password : string , newPassword : string , cb : Callback ) {
86- const connection = mysql . createConnection ( this . config ) ;
87-
88- if ( this . queries . update_user . length == 0 ) {
89- this . logger . info ( 'MySQL - Can\'t change password: update_user query is empty' ) ;
90- cb ( null , false )
91- return ;
99+ if ( this . queries . update_user . length == 0 && ! this . passwordQueryEmptyWarned ) {
100+ this . logger . warn ( { } , 'MySQL - Can\'t change password: update_user query is empty' ) ;
101+ this . passwordQueryEmptyWarned = true ;
102+ return cb ( null , false ) ;
92103 }
93104
94- connection . query ( this . queries . update_user , [ newPassword , user , password ] , ( error , result ) => {
105+ this . runQuery ( this . queries . update_user , [ newPassword , user , password ] , ( error , result ) => {
95106 if ( error ) {
107+ this . logger . error ( { error} , "MySQL - Error: @{error.message}" ) ;
96108 cb ( null , false ) ;
97109 }
98110 else {
99- cb ( null , result [ 0 ] . usergroups . split ( ',' ) ) ;
111+ cb ( null , true ) ;
100112 }
101-
102- connection . end ( ) ;
103113 } ) ;
104114 }
105115
106116 private async test ( ) : Promise < boolean > {
107117 return new Promise < boolean > ( ( resolve , reject ) => {
108- const connection = mysql . createConnection ( this . config ) ;
109118
110- connection . query ( 'SELECT 1' , ( err , res ) => {
119+ this . runQuery ( 'SELECT 1' , [ ] , ( err , res ) => {
111120 if ( err ) {
112- this . logger . error ( 'MySQL - Test connection did not work' ) ;
113- this . logger . error ( 'MySQL - Error: ' + err . message ) ;
121+ this . logger . error ( { } , 'MySQL - Test connection did not work' ) ;
122+ this . logger . error ( { err } , 'MySQL - Error: @{ err.message}' ) ;
114123 reject ( ) ;
115124 }
116- connection . destroy ( ) ;
117- resolve ( ) ;
125+ resolve ( true ) ;
118126 return ;
119- } )
127+ } ) ;
128+ } ) ;
129+ }
130+
131+ private runQuery ( query : string , parameters : string [ ] , callback : mysql . queryCallback ) : void {
132+ const connection = mysql . createConnection ( this . config ) ;
133+
134+ this . logger . debug ( { query, parameters} , 'MySQL - Running @{query} with @{parameters}' ) ;
135+ connection . query ( query , parameters , ( error , result ) => {
136+ if ( error ) {
137+ this . logger . error ( { error} , 'MySQL - Error: @{err.message}' ) ;
138+ }
139+ return callback ( error , result ) ;
120140 } ) ;
121141 }
122142}
0 commit comments