textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
82 lines (81 loc) • 3.2 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 });
const api_1 = require("../core/api");
/**
* Tokens is an API module for managing Cafe access tokens
*
* Tokens allow other peers to register with a Cafe peer. Use this API to create, list, validate,
* and remove tokens required for access to this Cafe.
*
* @extends API
*/
class Tokens extends api_1.API {
/**
* Creates an access token
*
* Generates an access token (44 random bytes) and saves a bcrypt hashed version for future
* lookup. The response contains a base58 encoded version of the random bytes token. If the
* ‘store’ option is set to false, the token is generated, but not stored in the local Cafe
* db. Alternatively, an existing token can be added using by specifying the ‘token’ option.
*
* @param token Use an existing token, rather than creating a new one
* @param store Whether to store the added/generated token to the local db (defaults to true)
* @see Cafes#add
* @returns New token as string
*/
add(token, store) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPost(`tokens`, undefined, {
token: token || '',
store: store || false
});
return response.text();
});
}
/**
* Check validity of existing cafe access token
*
* @param token Access token
* @returns Whether token is valid
*/
validate(token) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`tokens/${token}`);
return response.status === 200;
});
}
/**
* Retrieves information about all stored cafe tokens
*
* Only really useful for debugging. These are hashed tokens, so are not valid.
* @returns Array of bcrypt hashed tokens
*/
list() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet('tokens');
return response.json();
});
}
/**
* Removes an existing cafe token
*
* @param token Access token
* @returns Whether remove was successful
*/
remove(token) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendDelete(`tokens/${token}`);
return response.status === 204;
});
}
}
exports.default = Tokens;
;