UNPKG

4.16 kBJavaScriptView Raw
1const utils = require("../helpers/utils");
2const path = require("path");
3const request = require("request-promise");
4const credentialServerPort = utils.parseArgv("credentialServerPort", process.argv) || 3099;
5const SERVICE_URI = "http://localhost:" + credentialServerPort + "/credentials";
6
7/**
8 * Class representing Credential Manager
9 */
10class ServerCredentialManager {
11
12 /**
13 * Create pool of userIds based on creds object
14 * @param {Object} creds - set of user credentials
15 * @param {string} [pool] - name of pool
16 * @throws {Error}
17 * @example CredentialManager.createPool(credentials);
18 */
19 static createPool(creds, pool) {
20 let requestURI = SERVICE_URI;
21 if (pool) {
22 requestURI += "?pool=" + pool;
23 }
24
25 return request({
26 method: "POST",
27 uri: requestURI,
28 body: creds,
29 json: true
30 })
31 .catch(e => {
32 throw new Error("Credential pool has not been created")
33 })
34 }
35
36 /**
37 * Return free credentials from pool
38 * @return {Promise<Object>} - promise that resolves with set of credentials
39 * @throws {Error}
40 * @example
41 * CredentialManager.getCredentials();
42 * const currentCredentials = await CredentialManager.credentials;
43 */
44 static getCredentials(pool) {
45 let requestURI = SERVICE_URI;
46 if (pool) {
47 this.pool = pool;
48 requestURI += "?pool=" + pool;
49 }
50 this.credentials = request({
51 method: "GET",
52 uri: requestURI,
53 })
54 .then(body => JSON.parse(body))
55 .catch(e => {
56 throw e
57 })
58 }
59
60 /**
61 * Free credentials
62 * @throws {Error}
63 * @example CredentialManager.freeCredentials();
64 */
65 static freeCredentials() {
66 let requestURI = SERVICE_URI;
67 if (this.pool) {
68 requestURI += "?pool=" + pool;
69 }
70 return this.credentials.then(credentials => {
71 if (credentials) {
72 return request({
73 method: "PUT",
74 uri: requestURI,
75 body: {
76 username: credentials.username
77 },
78 json: true
79 })
80 }
81 })
82 .catch(e => {
83 throw e
84 })
85 }
86
87 /**
88 * Free credentials
89 * @param property - property to update
90 * @param value - value to update
91 * @param [pool] - pool to update
92 * @throws {Error}
93 * @example CredentialManager.updateProperty("cookie", "myCookie");
94 */
95 static updateProperty(property, value, pool) {
96 let requestURI = SERVICE_URI + "/update";
97 if (pool) {
98 requestURI += "?pool=" + pool;
99 }
100 return this.credentials.then(credentials => {
101 if (credentials) {
102 return request({
103 method: "PUT",
104 uri: requestURI,
105 body: {
106 username: credentials.username,
107 property: property,
108 value: value
109 },
110 json: true
111 })
112 }
113 })
114 .catch(e => {
115 throw e
116 })
117 }
118
119 /**
120 * Return specified credentials by username
121 * @param username - username to get
122 * @param [pool] - pool to update
123 * @return {Promise<Object>} - promise that resolves with set of credentials
124 * @throws {Error}
125 * @example
126 * CredentialManager.getCredentialsByUsername(ta1@email.com);
127 * const currentCredentials = await CredentialManager.credentials;
128 */
129 static getCredentialsByUsername(username, pool) {
130 let requestURI = SERVICE_URI + "/" + username;
131 if (pool) {
132 requestURI += "?pool=" + pool;
133 }
134 this.credentials = request({
135 method: "GET",
136 uri: requestURI,
137 })
138 .then(body => JSON.parse(body))
139 .catch(e => {
140 throw e
141 })
142 }
143
144}
145
146module.exports = ServerCredentialManager;
\No newline at end of file