credix
Version:
Official SDK for Credix Credit Management System
120 lines • 3.55 kB
JavaScript
import { ValidationError } from '../errors/index.js';
/**
* 認証ユーティリティクラス
*/
export class AuthManager {
constructor(config) {
// Lenient validation: non-empty, recognizable prefix. Server enforces full validity.
this.validateKeys(config.apiKey, config.secretKey);
this.apiKey = config.apiKey;
this.secretKey = config.secretKey;
}
/**
* APIキーとシークレットキーを検証
*/
validateKeys(apiKey, secretKey) {
if (!apiKey) {
throw new ValidationError('API key is required', 'apiKey');
}
if (!secretKey) {
throw new ValidationError('Secret key is required', 'secretKey');
}
// Allow both live and test prefixes; format specifics are enforced server-side
this.extractKeyType(apiKey);
}
/**
* APIキーから環境タイプを抽出
*/
extractKeyType(key) {
if (key.includes('credix_live_')) {
return 'live';
}
if (key.includes('credix_test_')) {
return 'test';
}
throw new ValidationError('Invalid key format', 'key');
}
/**
* APIキー情報を取得
*/
getKeyInfo() {
const type = this.extractKeyType(this.apiKey);
const prefix = this.apiKey.split('_').slice(0, 2).join('_');
return {
type,
projectId: '', // ProjectIDはサーバー側で解決される
isValid: true,
prefix,
};
}
/**
* 認証ヘッダーを生成
*/
getAuthHeaders() {
const headers = {
'X-API-Key': this.apiKey,
'X-API-Secret': this.secretKey,
'Content-Type': 'application/json',
};
return headers;
}
/**
* テスト環境かどうかを判定
*/
isTestMode() {
return this.extractKeyType(this.apiKey) === 'test';
}
/**
* 本番環境かどうかを判定
*/
isLiveMode() {
return this.extractKeyType(this.apiKey) === 'live';
}
/**
* APIキーを再生成(サーバーサイドのみ)
*/
static generateApiKey() {
// Generation helpers removed in slimmed SDK
throw new Error('Not supported in client SDK');
}
/**
* シークレットキーを再生成(サーバーサイドのみ)
* 64文字の完全ランダム文字列を生成
*/
static generateSecretKey() {
throw new Error('Not supported in client SDK');
}
/**
* キーペアを生成(サーバーサイドのみ)
*/
static generateKeyPair() {
throw new Error('Not supported in client SDK');
}
/**
* キーをマスク(表示用)
*/
static maskKey(_key) {
return '***';
}
/**
* キーの有効期限をチェック(将来の拡張用)
*/
checkKeyExpiration() {
// 将来的に有効期限機能を実装する場合のプレースホルダー
return {
isExpired: false,
};
}
/**
* キーのローテーション推奨をチェック
*/
shouldRotateKeys(lastRotatedAt) {
if (!lastRotatedAt) {
return false;
}
const daysSinceRotation = Math.floor((Date.now() - lastRotatedAt.getTime()) / (1000 * 60 * 60 * 24));
// 90日以上経過していたらローテーション推奨
return daysSinceRotation >= 90;
}
}
//# sourceMappingURL=auth.js.map