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
8 years ago
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'),
...
@@ -5,21 +5,55 @@ var passport = require('passport'),
safe
=
require
(
'safetydance'
),
safe
=
require
(
'safetydance'
),
bcrypt
=
require
(
'bcryptjs'
),
bcrypt
=
require
(
'bcryptjs'
),
uuid
=
require
(
'uuid/v4'
),
uuid
=
require
(
'uuid/v4'
),
redis
=
require
(
'redis'
),
BearerStrategy
=
require
(
'passport-http-bearer'
).
Strategy
,
BearerStrategy
=
require
(
'passport-http-bearer'
).
Strategy
,
LdapStrategy
=
require
(
'passport-ldapjs'
).
Strategy
,
LdapStrategy
=
require
(
'passport-ldapjs'
).
Strategy
,
HttpError
=
require
(
'connect-lastmile'
).
HttpError
,
HttpSuccess
=
require
(
'connect-lastmile'
).
HttpSuccess
;
HttpSuccess
=
require
(
'connect-lastmile'
).
HttpSuccess
;
var
LOCAL_AUTH_FILE
=
path
.
resolve
(
process
.
env
.
LOCAL_AUTH_FILE
||
'./.users.json'
);
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
()
{
function
issueAccessToken
()
{
return
function
(
req
,
res
,
next
)
{
return
function
(
req
,
res
,
next
)
{
var
accessToken
=
uuid
();
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
}));
next
(
new
HttpSuccess
(
201
,
{
accessToken
:
accessToken
,
user
:
req
.
user
}));
});
};
};
}
}
...
@@ -85,15 +119,22 @@ passport.use(new LdapStrategy(opts, function (profile, done) {
...
@@ -85,15 +119,22 @@ passport.use(new LdapStrategy(opts, function (profile, done) {
exports
.
verify
=
passport
.
authenticate
(
'bearer'
,
{
session
:
false
});
exports
.
verify
=
passport
.
authenticate
(
'bearer'
,
{
session
:
false
});
passport
.
use
(
new
BearerStrategy
(
function
(
token
,
done
)
{
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
)
{
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
,
{}));
next
(
new
HttpSuccess
(
200
,
{}));
});
};
};
exports
.
getProfile
=
function
(
req
,
res
,
next
)
{
exports
.
getProfile
=
function
(
req
,
res
,
next
)
{
...
...
This diff is collapsed.
Click to expand it.
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