import { _encodeUTF8 , _encodeBase64URLFromByteArray } from "@skygear/core" ; import { signAnonymousToken , getAnonymousKey } from "./nativemodule" ; function encodeRawBase64URL ( input ) { return _encodeBase64URLFromByteArray ( _encodeUTF8 ( input ) ) ; } function toRawBase64URL ( base64 ) { return base64 . replace ( /=+$/ , "" ) . replace ( /\//g , "_" ) . replace ( /\+/g , "-" ) ; } export async function getAnonymousJWK ( kid ) { const key = await getAnonymousKey ( kid ) ; if ( key . jwk ) { key . jwk . n = toRawBase64URL ( key . jwk . n ) ; key . jwk . e = toRawBase64URL ( key . jwk . e ) ; } return key ; } export async function signAnonymousJWT ( kid , header , payload ) { const dataToSign = [ header , payload ] . map ( part => encodeRawBase64URL ( JSON . stringify ( part ) ) ) . join ( "." ) ; const sig = toRawBase64URL ( await signAnonymousToken ( kid , dataToSign ) ) ; const token = `${ dataToSign }.${ sig }` ; return token ; }