2021-08-26 14:37:55 +02:00
'use strict' ;
const crypto = require ( 'crypto' ) ;
2021-08-27 09:44:29 +02:00
/ * *
* @ typedef ApiToken
*
* @ property { number } id
* @ property { string } name
* @ property { string } [ description ]
* @ property { string } accessKey
2021-08-27 10:30:18 +02:00
* @ property { 'read-only' | 'full-access' } type
2021-08-27 09:44:29 +02:00
* /
2021-08-26 14:37:55 +02:00
/ * *
2021-08-27 16:23:19 +02:00
* @ param { Object } whereParams
* @ param { string } whereParams . name
* @ param { string } [ whereParams . description ]
2021-08-26 14:37:55 +02:00
*
2021-08-27 10:30:18 +02:00
* @ returns { Promise < boolean > }
2021-08-26 14:37:55 +02:00
* /
2021-08-27 16:23:19 +02:00
const exists = async ( whereParams = { } ) => {
const apiToken = await strapi . query ( 'admin::api-token' ) . findOne ( { where : whereParams } ) ;
return ! ! apiToken ;
} ;
/ * *
* @ param { string } accessKey
*
* @ returns { string }
* /
const hash = accessKey => {
return crypto
2021-08-27 17:06:05 +02:00
. createHmac ( 'sha512' , strapi . config . get ( 'server.admin.api-token.salt' ) )
. update ( accessKey )
2021-08-27 16:23:19 +02:00
. digest ( 'hex' ) ;
2021-08-26 14:37:55 +02:00
} ;
/ * *
* @ param { Object } attributes
* @ param { 'read-only' | 'full-access' } attributes . type
* @ param { string } attributes . name
* @ param { string } [ attributes . description ]
*
2021-08-27 09:44:29 +02:00
* @ returns { Promise < ApiToken > }
2021-08-26 14:37:55 +02:00
* /
const create = async attributes => {
const accessKey = crypto . randomBytes ( 128 ) . toString ( 'hex' ) ;
2021-08-27 16:23:19 +02:00
const apiToken = await strapi . query ( 'admin::api-token' ) . create ( {
select : [ 'id' , 'name' , 'description' , 'type' ] ,
2021-08-26 14:37:55 +02:00
data : {
... attributes ,
2021-08-27 16:23:19 +02:00
accessKey : hash ( accessKey ) ,
2021-08-26 14:37:55 +02:00
} ,
} ) ;
2021-08-27 16:23:19 +02:00
return {
... apiToken ,
accessKey ,
} ;
} ;
/ * *
* @ returns { void }
* /
const createSaltIfNotDefined = ( ) => {
if ( strapi . config . get ( 'server.admin.api-token.salt' ) ) {
return ;
}
2021-08-27 16:47:02 +02:00
if ( process . env . API _TOKEN _SALT ) {
throw new Error (
` There's something wrong with the configuration of your api-token salt. If you have changed the env variable used in the configuration file, please verify that you have created and set the variable in your .env file. `
) ;
2021-08-27 16:23:19 +02:00
}
2021-08-27 16:47:02 +02:00
const salt = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
strapi . fs . appendFile ( '.env' , ` API_TOKEN_SALT= ${ salt } \n ` ) ;
strapi . config . set ( 'server.admin.api-token.salt' , salt ) ;
2021-08-26 14:37:55 +02:00
} ;
2021-08-27 08:14:36 +02:00
/ * *
* @ returns { Promise < ApiToken [ ] > }
* /
const list = async ( ) => {
2021-08-27 08:39:08 +02:00
return strapi . query ( 'strapi::api-token' ) . findMany ( {
select : [ 'id' , 'name' , 'description' , 'type' , 'accessKey' ] ,
orderBy : { name : 'ASC' } ,
} ) ;
2021-08-27 08:14:36 +02:00
} ;
2021-08-26 14:37:55 +02:00
module . exports = {
create ,
exists ,
2021-08-27 16:23:19 +02:00
createSaltIfNotDefined ,
hash ,
2021-08-27 08:14:36 +02:00
list ,
2021-08-26 14:37:55 +02:00
} ;