2020-12-28 15:04:52 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
|
|
* This file is part of Auth-Server-Helper which is released under MIT.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, December 2020
|
|
|
|
*/
|
|
|
|
|
2020-12-19 15:40:49 +01:00
|
|
|
import {
|
|
|
|
create_salt,
|
|
|
|
sign_object,
|
|
|
|
verify_signature_get_info
|
|
|
|
} from '@sapphirecode/crypto-helper';
|
|
|
|
import keystore from './KeyStore';
|
|
|
|
import blacklist from './Blacklist';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-shadow
|
|
|
|
type TokenType = 'access_token'|'refresh_token'|'part_token'|'none'
|
|
|
|
|
|
|
|
interface VerificationResult {
|
|
|
|
authorized: boolean;
|
2020-12-28 14:53:14 +01:00
|
|
|
valid: boolean;
|
2020-12-19 15:40:49 +01:00
|
|
|
type: TokenType;
|
2020-12-28 14:53:14 +01:00
|
|
|
next_module?: string;
|
2020-12-19 15:40:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface SignatureResult {
|
|
|
|
signature: string;
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Authority {
|
|
|
|
public verify (key: string): VerificationResult {
|
2020-12-19 16:19:09 +01:00
|
|
|
const result: VerificationResult = {
|
2020-12-28 14:53:14 +01:00
|
|
|
authorized: false,
|
|
|
|
valid: false,
|
|
|
|
type: 'none'
|
2020-12-19 16:19:09 +01:00
|
|
|
};
|
2020-12-19 15:40:49 +01:00
|
|
|
const data = verify_signature_get_info (
|
|
|
|
key,
|
|
|
|
(info) => keystore.get_key (info.iat / 1000),
|
|
|
|
(info) => info.valid_for * 1000
|
|
|
|
);
|
|
|
|
|
|
|
|
if (data === null)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result.type = data.type;
|
|
|
|
|
|
|
|
if (!blacklist.is_valid (data.id))
|
|
|
|
return result;
|
|
|
|
|
2020-12-28 14:53:14 +01:00
|
|
|
result.valid = true;
|
2020-12-19 15:40:49 +01:00
|
|
|
result.authorized = result.type === 'access_token';
|
|
|
|
result.next_module = data.obj;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public sign (
|
|
|
|
type: TokenType,
|
|
|
|
valid_for: number,
|
|
|
|
next_module?: string
|
|
|
|
): SignatureResult {
|
|
|
|
const time = Date.now ();
|
2020-12-19 16:19:09 +01:00
|
|
|
const key = keystore.get_key (time / 1000, valid_for);
|
2020-12-19 15:40:49 +01:00
|
|
|
const attributes = {
|
|
|
|
id: create_salt (),
|
|
|
|
iat: time,
|
|
|
|
type,
|
|
|
|
valid_for
|
|
|
|
};
|
|
|
|
const signature = sign_object (next_module, key, attributes);
|
|
|
|
return { id: attributes.id, signature };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auth = (new Authority);
|
|
|
|
|
|
|
|
export default auth;
|