X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git;a=blobdiff_plain;f=src%2Fauth.js;h=a885d492eb2a68cf5f9e572b02fc01188d01ca40;hp=25326889a78affd4173687869ff0765d4bd4582e;hb=c2c00fca7dccb6e512a0f01bc87db129538765ef;hpb=d5940df06a4f498176dad293f66607fb69eb2a28 diff --git a/src/auth.js b/src/auth.js index 2532688..a885d49 100644 --- a/src/auth.js +++ b/src/auth.js @@ -15,6 +15,8 @@ const LDAP_USERS_BASE_DN = process.env.CLOUDRON_LDAP_USERS_BASE_DN; const LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json'); const TOKENSTORE_FILE = path.resolve(process.env.TOKENSTORE_FILE || './.tokens.json'); const AUTH_METHOD = (LDAP_URL && LDAP_USERS_BASE_DN) ? 'ldap' : 'local'; +const LOGIN_TOKEN_PREFIX = 'login-'; +const API_TOKEN_PREFIX = 'api-'; if (AUTH_METHOD === 'ldap') { console.log('Use ldap auth'); @@ -34,8 +36,11 @@ var tokenStore = { get: function (token, callback) { callback(tokenStore.data[token] ? null : 'not found', tokenStore.data[token]); }, - set: function (token, data, callback) { - tokenStore.data[token] = data; + getApiTokens: function (callback) { + callback(null, Object.keys(tokenStore.data).filter(function (t) { return t.indexOf(API_TOKEN_PREFIX) === 0; })) + }, + set: function (token, user, callback) { + tokenStore.data[token] = user; tokenStore.save(); callback(null); }, @@ -102,7 +107,7 @@ exports.login = function (req, res, next) { verifyUser(req.body.username, req.body.password, function (error, user) { if (error) return next(new HttpError(401, 'Invalid credentials')); - var accessToken = uuid(); + var accessToken = LOGIN_TOKEN_PREFIX + uuid(); tokenStore.set(accessToken, user, function (error) { if (error) return next(new HttpError(500, error)); @@ -139,6 +144,32 @@ exports.getProfile = function (req, res, next) { next(new HttpSuccess(200, { username: req.user.username })); }; +exports.getTokens = function (req, res, next) { + tokenStore.getApiTokens(function (error, result) { + if (error) return next(new HttpError(500, error)); + + next(new HttpSuccess(200, { accessTokens: result })); + }); +}; + +exports.createToken = function (req, res, next) { + var accessToken = API_TOKEN_PREFIX + uuid(); + + tokenStore.set(accessToken, req.user, function (error) { + if (error) return next(new HttpError(500, error)); + + next(new HttpSuccess(201, { accessToken: accessToken })); + }); +}; + +exports.delToken = function (req, res, next) { + tokenStore.del(req.params.token, function (error) { + if (error) console.error(error); + + next(new HttpSuccess(200, {})); + }); +}; + // webdav usermanager exports.WebdavUserManager = WebdavUserManager;