2018-08-27 00:16:13 +02:00
/ * *
* @ author gchq77703 [ ]
* @ copyright Crown Copyright 2018
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2018-08-27 00:16:13 +02:00
import jwt from "jsonwebtoken" ;
2019-07-09 13:23:59 +02:00
import OperationError from "../errors/OperationError.mjs" ;
2018-08-27 00:16:13 +02:00
/ * *
* JWT Verify operation
* /
class JWTVerify extends Operation {
/ * *
* JWTVerify constructor
* /
constructor ( ) {
super ( ) ;
this . name = "JWT Verify" ;
this . module = "Crypto" ;
2018-08-31 15:58:06 +02:00
this . description = "Verifies that a JSON Web Token is valid and has been signed with the provided secret / private key.<br><br>The key should be either the secret for HMAC algorithms or the PEM-encoded private key for RSA and ECDSA." ;
this . infoURL = "https://wikipedia.org/wiki/JSON_Web_Token" ;
2018-08-27 00:16:13 +02:00
this . inputType = "string" ;
this . outputType = "JSON" ;
this . args = [
{
2018-08-31 15:58:06 +02:00
name : "Private/Secret Key" ,
2018-08-29 23:43:10 +02:00
type : "text" ,
2018-08-31 15:58:06 +02:00
value : "secret"
2018-08-27 00:16:13 +02:00
} ,
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
const [ key ] = args ;
try {
2018-08-31 15:58:06 +02:00
const verified = jwt . verify ( input , key , { algorithms : [
2018-08-29 23:43:10 +02:00
"HS256" ,
"HS384" ,
"HS512" ,
2021-02-06 08:27:54 +01:00
"RS256" ,
2021-02-06 08:58:49 +01:00
"RS384" ,
2021-02-06 08:27:54 +01:00
"RS512" ,
2021-02-06 08:58:49 +01:00
"ES256" ,
"ES384" ,
"ES512" ,
2018-08-29 23:43:10 +02:00
"none"
] } ) ;
2018-08-31 15:58:06 +02:00
2019-07-05 13:22:52 +02:00
if ( Object . prototype . hasOwnProperty . call ( verified , "name" ) && verified . name === "JsonWebTokenError" ) {
2018-08-31 15:58:06 +02:00
throw new OperationError ( verified . message ) ;
}
return verified ;
2018-08-27 00:16:13 +02:00
} catch ( err ) {
2018-08-31 15:58:06 +02:00
throw new OperationError ( err ) ;
2018-08-27 00:16:13 +02:00
}
}
}
export default JWTVerify ;