mirror of
https://github.com/titanscouting/tra-analysis.git
synced 2025-07-27 13:18:49 +00:00
apps
data analysis
website
functions
node_modules
.bin
@firebase
@google-cloud
@grpc
@mrmlnc
@nodelib
@protobufjs
@types
accepts
acorn
acorn-es7-plugin
ajv
ansi-regex
arr-diff
arr-flatten
arr-union
array-filter
array-flatten
array-union
array-uniq
array-unique
arrify
ascli
asn1
assert-plus
assign-symbols
async
asynckit
atob
aws-sign2
aws4
axios
balanced-match
base
bcrypt-pbkdf
body-parser
brace-expansion
braces
buffer-equal-constant-time
buffer-from
bun
bytebuffer
bytes
cache-base
call-me-maybe
call-signature
camelcase
capture-stack-trace
caseless
class-utils
cliui
code-point-at
collection-visit
colour
combined-stream
component-emitter
compressible
concat-map
concat-stream
configstore
content-disposition
content-type
cookie
cookie-signature
copy-descriptor
core-js
core-util-is
cors
create-error-class
crypto-random-string
dashdash
debug
decamelize
decode-uri-component
deep-equal
define-properties
define-property
delayed-stream
depd
destroy
diff-match-patch
dir-glob
dom-storage
dot-prop
duplexify
eastasianwidth
ecc-jsbn
ecdsa-sig-formatter
ee-first
empower
empower-core
encodeurl
end-of-stream
ent
escape-html
espurify
estraverse
etag
expand-brackets
express
extend
extend-shallow
extglob
extsprintf
fast-deep-equal
fast-glob
fast-json-stable-stringify
faye-websocket
fill-range
finalhandler
firebase-admin
firebase-functions
follow-redirects
for-in
forever-agent
form-data
forwarded
fragment-cache
fresh
fs.realpath
functional-red-black-tree
gcp-metadata
gcs-resumable-upload
get-value
getpass
glob
glob-parent
glob-to-regexp
globby
google-auth-library
google-auto-auth
google-gax
google-p12-pem
google-proto-files
graceful-fs
grpc
gtoken
har-schema
har-validator
has-value
has-values
hash-stream-validation
http-errors
http-parser-js
http-signature
iconv-lite
ignore
imurmurhash
indexof
inflight
inherits
invert-kv
ipaddr.js
is
is-accessor-descriptor
is-buffer
is-data-descriptor
is-descriptor
is-extendable
is-extglob
is-fullwidth-code-point
is-glob
is-number
is-obj
is-plain-object
is-stream-ended
is-typedarray
is-windows
isarray
isobject
isstream
jsbn
json-schema
json-schema-traverse
json-stringify-safe
jsonwebtoken
lib
CHANGELOG.md
LICENSE
README.md
decode.js
index.js
package.json
sign.js
verify.js
jsprim
jwa
jws
kind-of
lcid
lodash
lodash.camelcase
lodash.clone
lodash.includes
lodash.isboolean
lodash.isinteger
lodash.isnumber
lodash.isplainobject
lodash.isstring
lodash.merge
lodash.once
log-driver
long
lru-cache
make-dir
map-cache
map-visit
media-typer
merge-descriptors
merge2
methmeth
methods
micromatch
mime
mime-db
mime-types
minimatch
mixin-deep
modelo
ms
nan
nanomatch
negotiator
node-forge
number-is-nan
oauth-sign
object-assign
object-copy
object-keys
object-visit
object.pick
on-finished
once
optjs
os-locale
parseurl
pascalcase
path-dirname
path-is-absolute
path-to-regexp
path-type
performance-now
pify
posix-character-classes
power-assert
power-assert-context-formatter
power-assert-context-reducer-ast
power-assert-context-traversal
power-assert-formatter
power-assert-renderer-assertion
power-assert-renderer-base
power-assert-renderer-comparison
power-assert-renderer-diagram
power-assert-renderer-file
power-assert-util-string-width
process-nextick-args
protobufjs
proxy-addr
pseudomap
psl
pump
pumpify
punycode
qs
range-parser
raw-body
readable-stream
regex-not
repeat-element
repeat-string
request
resolve-url
ret
retry-axios
retry-request
safe-buffer
safe-regex
safer-buffer
send
serve-static
set-value
setprototypeof
signal-exit
slash
snakeize
snapdragon
snapdragon-node
snapdragon-util
source-map
source-map-resolve
source-map-url
split-array-stream
split-string
sshpk
static-extend
statuses
stream-events
stream-shift
string-format-obj
string-width
string_decoder
stringifier
strip-ansi
stubs
through2
to-object-path
to-regex
to-regex-range
tough-cookie
traverse
tslib
tunnel-agent
tweetnacl
type-is
type-name
typedarray
union-value
unique-string
universal-deep-strict-equal
unpipe
unset-value
uri-js
urix
use
util-deprecate
utils-merge
uuid
vary
verror
websocket-driver
websocket-extensions
window-size
wrap-ansi
wrappy
write-file-atomic
xdg-basedir
xmlhttprequest
xtend
y18n
yallist
yargs
index.js
package-lock.json
package.json
node_modules
public
.firebaserc
.gitignore
.runtimeconfig.json
firebase.json
firestore.indexes.json
firestore.rules
package-lock.json
.gitattributes
.gitignore
182 lines
5.3 KiB
JavaScript
182 lines
5.3 KiB
JavaScript
var JsonWebTokenError = require('./lib/JsonWebTokenError');
|
|
var NotBeforeError = require('./lib/NotBeforeError');
|
|
var TokenExpiredError = require('./lib/TokenExpiredError');
|
|
var decode = require('./decode');
|
|
var timespan = require('./lib/timespan');
|
|
var jws = require('jws');
|
|
var xtend = require('xtend');
|
|
|
|
module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
|
if ((typeof options === 'function') && !callback) {
|
|
callback = options;
|
|
options = {};
|
|
}
|
|
|
|
if (!options) {
|
|
options = {};
|
|
}
|
|
|
|
//clone this object since we are going to mutate it.
|
|
options = xtend(options);
|
|
var done;
|
|
|
|
if (callback) {
|
|
done = callback;
|
|
} else {
|
|
done = function(err, data) {
|
|
if (err) throw err;
|
|
return data;
|
|
};
|
|
}
|
|
|
|
if (options.clockTimestamp && typeof options.clockTimestamp !== 'number') {
|
|
return done(new JsonWebTokenError('clockTimestamp must be a number'));
|
|
}
|
|
|
|
var clockTimestamp = options.clockTimestamp || Math.floor(Date.now() / 1000);
|
|
|
|
if (!jwtString){
|
|
return done(new JsonWebTokenError('jwt must be provided'));
|
|
}
|
|
|
|
if (typeof jwtString !== 'string') {
|
|
return done(new JsonWebTokenError('jwt must be a string'));
|
|
}
|
|
|
|
var parts = jwtString.split('.');
|
|
|
|
if (parts.length !== 3){
|
|
return done(new JsonWebTokenError('jwt malformed'));
|
|
}
|
|
|
|
var hasSignature = parts[2].trim() !== '';
|
|
|
|
if (!hasSignature && secretOrPublicKey){
|
|
return done(new JsonWebTokenError('jwt signature is required'));
|
|
}
|
|
|
|
if (hasSignature && !secretOrPublicKey) {
|
|
return done(new JsonWebTokenError('secret or public key must be provided'));
|
|
}
|
|
|
|
if (!hasSignature && !options.algorithms) {
|
|
options.algorithms = ['none'];
|
|
}
|
|
|
|
if (!options.algorithms) {
|
|
options.algorithms = ~secretOrPublicKey.toString().indexOf('BEGIN CERTIFICATE') ||
|
|
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?
|
|
[ 'RS256','RS384','RS512','ES256','ES384','ES512' ] :
|
|
~secretOrPublicKey.toString().indexOf('BEGIN RSA PUBLIC KEY') ?
|
|
[ 'RS256','RS384','RS512' ] :
|
|
[ 'HS256','HS384','HS512' ];
|
|
|
|
}
|
|
|
|
var decodedToken;
|
|
try {
|
|
decodedToken = jws.decode(jwtString);
|
|
} catch(err) {
|
|
return done(err);
|
|
}
|
|
|
|
if (!decodedToken) {
|
|
return done(new JsonWebTokenError('invalid token'));
|
|
}
|
|
|
|
var header = decodedToken.header;
|
|
|
|
if (!~options.algorithms.indexOf(header.alg)) {
|
|
return done(new JsonWebTokenError('invalid algorithm'));
|
|
}
|
|
|
|
var valid;
|
|
|
|
try {
|
|
valid = jws.verify(jwtString, header.alg, secretOrPublicKey);
|
|
} catch (e) {
|
|
return done(e);
|
|
}
|
|
|
|
if (!valid)
|
|
return done(new JsonWebTokenError('invalid signature'));
|
|
|
|
var payload;
|
|
|
|
try {
|
|
payload = decode(jwtString);
|
|
} catch(err) {
|
|
return done(err);
|
|
}
|
|
|
|
if (typeof payload.nbf !== 'undefined' && !options.ignoreNotBefore) {
|
|
if (typeof payload.nbf !== 'number') {
|
|
return done(new JsonWebTokenError('invalid nbf value'));
|
|
}
|
|
if (payload.nbf > clockTimestamp + (options.clockTolerance || 0)) {
|
|
return done(new NotBeforeError('jwt not active', new Date(payload.nbf * 1000)));
|
|
}
|
|
}
|
|
|
|
if (typeof payload.exp !== 'undefined' && !options.ignoreExpiration) {
|
|
if (typeof payload.exp !== 'number') {
|
|
return done(new JsonWebTokenError('invalid exp value'));
|
|
}
|
|
if (clockTimestamp >= payload.exp + (options.clockTolerance || 0)) {
|
|
return done(new TokenExpiredError('jwt expired', new Date(payload.exp * 1000)));
|
|
}
|
|
}
|
|
|
|
if (options.audience) {
|
|
var audiences = Array.isArray(options.audience)? options.audience : [options.audience];
|
|
var target = Array.isArray(payload.aud) ? payload.aud : [payload.aud];
|
|
|
|
var match = target.some(function(targetAudience) {
|
|
return audiences.some(function(audience) {
|
|
return audience instanceof RegExp ? audience.test(targetAudience) : audience === targetAudience;
|
|
});
|
|
});
|
|
|
|
if (!match)
|
|
return done(new JsonWebTokenError('jwt audience invalid. expected: ' + audiences.join(' or ')));
|
|
}
|
|
|
|
if (options.issuer) {
|
|
var invalid_issuer =
|
|
(typeof options.issuer === 'string' && payload.iss !== options.issuer) ||
|
|
(Array.isArray(options.issuer) && options.issuer.indexOf(payload.iss) === -1);
|
|
|
|
if (invalid_issuer) {
|
|
return done(new JsonWebTokenError('jwt issuer invalid. expected: ' + options.issuer));
|
|
}
|
|
}
|
|
|
|
if (options.subject) {
|
|
if (payload.sub !== options.subject) {
|
|
return done(new JsonWebTokenError('jwt subject invalid. expected: ' + options.subject));
|
|
}
|
|
}
|
|
|
|
if (options.jwtid) {
|
|
if (payload.jti !== options.jwtid) {
|
|
return done(new JsonWebTokenError('jwt jwtid invalid. expected: ' + options.jwtid));
|
|
}
|
|
}
|
|
|
|
if (options.maxAge) {
|
|
if (typeof payload.iat !== 'number') {
|
|
return done(new JsonWebTokenError('iat required when maxAge is specified'));
|
|
}
|
|
|
|
var maxAgeTimestamp = timespan(options.maxAge, payload.iat);
|
|
if (typeof maxAgeTimestamp === 'undefined') {
|
|
return done(new JsonWebTokenError('"maxAge" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'));
|
|
}
|
|
if (clockTimestamp >= maxAgeTimestamp + (options.clockTolerance || 0)) {
|
|
return done(new TokenExpiredError('maxAge exceeded', new Date(maxAgeTimestamp * 1000)));
|
|
}
|
|
}
|
|
|
|
return done(null, payload);
|
|
};
|