@chevre/domain
Version:
Chevre Domain Library for Node.js
71 lines (70 loc) • 2.97 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialsRepo = void 0;
/**
* 認証情報リポジトリ
*/
class CredentialsRepo {
constructor(redisClient, options) {
this.redisClient = redisClient;
this.options = options;
}
save(credentials) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof this.options.expireInSeconds !== 'number' || this.options.expireInSeconds <= 0) {
// expireInSeconds:0に設定すれば保管しない
return;
}
// const { access_token, expired_at } = credentials;
if (typeof credentials.access_token !== 'string') {
throw new Error('access_token must be string');
}
// if (typeof credentials.expired_at !== 'string') {
// throw new Error('expired_at must be string');
// }
const key = this.createKey();
const value = JSON.stringify(credentials);
const multi = this.redisClient.multi();
yield multi.set(key, value)
// .expireAt(key, Number(expired_at))
.expire(key, this.options.expireInSeconds)
.exec();
// if (Array.isArray(results) && (results[0] === 1 || (<any>results)[0] === true)) {
// return true;
// } else {
// throw new Error('unexpected');
// }
});
}
find() {
return __awaiter(this, void 0, void 0, function* () {
const key = this.createKey();
let credentials;
try {
const credentialsStr = yield this.redisClient.get(key);
if (typeof credentialsStr === 'string') {
credentials = JSON.parse(credentialsStr);
}
}
catch (error) {
// tslint:disable-next-line:no-console
console.error('credential parse error:', error);
}
return credentials;
});
}
createKey() {
return `${CredentialsRepo.KEY_PREFIX}:${this.options.scope}`;
}
}
exports.CredentialsRepo = CredentialsRepo;
CredentialsRepo.KEY_PREFIX = 'credentials';
;