Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
PUBLIC
/
surfer-okd
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit bcee8931
authored
Feb 09, 2017
by
Johannes Zellner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use optionally redis if available
1 parent
58339c49
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
6 deletions
src/auth.js
src/auth.js
View file @
bcee893
...
...
@@ -5,21 +5,55 @@ var passport = require('passport'),
safe
=
require
(
'safetydance'
),
bcrypt
=
require
(
'bcryptjs'
),
uuid
=
require
(
'uuid/v4'
),
redis
=
require
(
'redis'
),
BearerStrategy
=
require
(
'passport-http-bearer'
).
Strategy
,
LdapStrategy
=
require
(
'passport-ldapjs'
).
Strategy
,
HttpError
=
require
(
'connect-lastmile'
).
HttpError
,
HttpSuccess
=
require
(
'connect-lastmile'
).
HttpSuccess
;
var
LOCAL_AUTH_FILE
=
path
.
resolve
(
process
.
env
.
LOCAL_AUTH_FILE
||
'./.users.json'
);
var
gTokenStore
=
{};
var
tokenStore
=
{
data
:
{},
get
:
function
(
token
,
callback
)
{
callback
(
tokenStore
.
data
[
token
]
?
null
:
'not found'
,
tokenStore
.
data
[
token
]);
},
set
:
function
(
token
,
data
,
callback
)
{
tokenStore
.
data
[
token
]
=
data
;
callback
(
null
);
},
del
:
function
(
token
,
callback
)
{
delete
tokenStore
.
data
[
token
];
callback
(
null
);
}
};
if
(
process
.
env
.
REDIS_URL
)
{
console
.
log
(
'Enable redis token store'
);
var
redisClient
=
redis
.
createClient
(
process
.
env
.
REDIS_URL
);
if
(
process
.
env
.
REDIS_PASSWORD
)
{
console
.
log
(
'Using redis auth'
);
redisClient
.
auth
(
process
.
env
.
REDIS_PASSWORD
);
}
// overwrite the tokenStore api
tokenStore
.
get
=
redisClient
.
get
.
bind
(
redisClient
);
tokenStore
.
set
=
redisClient
.
set
.
bind
(
redisClient
);
tokenStore
.
del
=
redisClient
.
del
.
bind
(
redisClient
);
}
else
{
console
.
log
(
'Use in-memory token store'
);
}
function
issueAccessToken
()
{
return
function
(
req
,
res
,
next
)
{
var
accessToken
=
uuid
();
gTokenStore
[
accessToken
]
=
req
.
user
;
tokenStore
.
set
(
accessToken
,
req
.
user
,
function
(
error
)
{
if
(
error
)
return
next
(
new
HttpError
(
500
,
error
));
next
(
new
HttpSuccess
(
201
,
{
accessToken
:
accessToken
,
user
:
req
.
user
}));
});
};
}
...
...
@@ -85,15 +119,22 @@ passport.use(new LdapStrategy(opts, function (profile, done) {
exports
.
verify
=
passport
.
authenticate
(
'bearer'
,
{
session
:
false
});
passport
.
use
(
new
BearerStrategy
(
function
(
token
,
done
)
{
if
(
!
gTokenStore
[
token
])
return
done
(
null
,
false
);
tokenStore
.
get
(
token
,
function
(
error
,
result
)
{
if
(
error
)
{
console
.
error
(
error
);
return
done
(
null
,
false
);
}
return
done
(
null
,
gTokenStore
[
token
],
{
accessToken
:
token
});
done
(
null
,
result
,
{
accessToken
:
token
});
});
}));
exports
.
logout
=
function
(
req
,
res
,
next
)
{
delete
gTokenStore
[
req
.
authInfo
.
accessToken
];
tokenStore
.
del
(
req
.
authInfo
.
accessToken
,
function
(
error
)
{
if
(
error
)
console
.
error
(
error
);
next
(
new
HttpSuccess
(
200
,
{}));
});
};
exports
.
getProfile
=
function
(
req
,
res
,
next
)
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment