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

Fix/tokens #335

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
refactor auth endpoint
Signed-off-by: Denys Bitaccess <[email protected]>
Denys Bitaccess committed Dec 1, 2021

Unverified

No user is associated with the committer email.
commit 3ca5c2afc850e1e6f4878d9823808e3a25c5ff0a
46 changes: 18 additions & 28 deletions controllers/api.js
Original file line number Diff line number Diff line change
@@ -159,38 +159,28 @@ router.post('/create', postLimiter, async function (req, res) {

router.post('/auth', postLimiter, async function (req, res) {
logger.log('/auth', [req.id]);
if (!((req.body.login && req.body.password) || req.body.refresh_token)) return errorBadArguments(res);

let u = new User(redis, bitcoinclient, lightning);
const u = new User(redis, bitcoinclient, lightning);

if (req.body.refresh_token) {
// need to refresh token
if (await u.loadByRefreshToken(req.body.refresh_token)) {
const body = {
token_type: 'bearer',
refresh_token: u.getRefreshToken(),
access_token: u.getAccessToken(),
expires_in: accessTokenLifeTime,
}
res.send(body);
} else {
return errorBadAuth(res);
}
let authenticated = false
if (req.body.login && req.body.password) {
authenticated = await u.loadByLoginAndPassword(req.body.login, req.body.password);
} else if (req.body.refresh_token) {
authenticated = await u.loadByRefreshToken(req.body.refresh_token)
} else {
// need to authorize user
let result = await u.loadByLoginAndPassword(req.body.login, req.body.password);
if (result) {
const body = {
token_type: 'bearer',
refresh_token: u.getRefreshToken(),
access_token: u.getAccessToken(),
expires_in: accessTokenLifeTime
}
res.send(body);
} else {
errorBadAuth(res);
}
return errorBadArguments(res);
}

if (!authenticated) {
return errorBadAuth(res);
}

return res.send({
token_type: 'bearer',
refresh_token: u.getRefreshToken(),
access_token: u.getAccessToken(),
expires_in: accessTokenLifeTime,
});
});

router.post('/addinvoice', postLimiter, async function (req, res) {