Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduces per-user API tokens via 'apikey: true' #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ export LDAP_SEARCH_BASE=<LDAP_SEARCH_BASE>
export LDAP_SEARCH_FILTER=<LDAP_SEARCH_FILTER>
```

Additionally, the following environment variable can toggle the global access key.

If **not set** or **set to an empty string** only user-level access keys will be accepted.

If **set to a string** the value will act as a global plain-text key.

:warning: This setting is insecure and should only be used when bootstrapping a new environment.

```
CLAY_ACCESS_KEY=
```

## License

MIT
14 changes: 10 additions & 4 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { encode } = require('../utils'),
const { encode, generateAPIKey } = require('../utils'),
encrypt = require('../services/encrypt');
let db = require('../services/storage'),
bus;
Expand All @@ -11,8 +11,8 @@ let db = require('../services/storage'),
* @returns {Promise}
*/
function createUser(data = {}) {
const { username, password, provider, auth } = data;
let uri = '/_users/';
const { username, password, provider, auth, apikey } = data;
let uri = '/_users/', key = { apikey: null, hash: null };

// Validate payload
if (!username || !provider || !auth) {
Expand All @@ -23,6 +23,12 @@ function createUser(data = {}) {
data.password = encrypt.hashPassword(password);
}

if (apikey === true) {
key = generateAPIKey(username, provider);
}

data.apikey = key.hash;

// Add the encoded username and provider to the end of the uri
uri += encode(username.toLowerCase(), provider);

Expand All @@ -31,7 +37,7 @@ function createUser(data = {}) {
data._ref = uri;

bus.publish('saveUser', { key: uri, value: data });
return data;
return { ...data, apikey: key.apikey };
});
}

Expand Down
Loading