Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
arsnova-click-v2-backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
ARSnova
arsnova-click-v2-backend
Commits
ab3eb3df
Commit
ab3eb3df
authored
Jul 08, 2019
by
Christopher Mark Fullarton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds token authentication mechanism
parent
683c2d37
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
52 additions
and
5 deletions
+52
-5
src/db/UserDAO.ts
src/db/UserDAO.ts
+8
-0
src/entities/UserEntity.ts
src/entities/UserEntity.ts
+12
-0
src/interfaces/users/IUserBase.ts
src/interfaces/users/IUserBase.ts
+1
-0
src/models/UserModelItem/UserModel.ts
src/models/UserModelItem/UserModel.ts
+2
-1
src/routers/rest/AdminRouter.ts
src/routers/rest/AdminRouter.ts
+2
-0
src/routers/rest/LibRouter.ts
src/routers/rest/LibRouter.ts
+26
-4
src/tests/routes/expiry-quiz.test.ts
src/tests/routes/expiry-quiz.test.ts
+1
-0
No files found.
src/db/UserDAO.ts
View file @
ab3eb3df
...
...
@@ -94,6 +94,14 @@ class UserDAO extends AbstractDAO<{ [key: string]: IUserEntity }> {
return
this
.
storage
[
name
];
}
public
getUserByTokenHash
(
tokenHash
:
string
):
IUserEntity
{
if
(
this
.
isEmptyVars
(
tokenHash
))
{
return
null
;
}
return
Object
.
values
(
this
.
storage
).
find
(
user
=>
user
.
tokenHash
===
tokenHash
);
}
public
getUserById
(
id
:
ObjectId
):
IUserEntity
{
return
Object
.
values
(
this
.
storage
).
find
(
val
=>
val
.
id
.
equals
(
id
));
}
...
...
src/entities/UserEntity.ts
View file @
ab3eb3df
...
...
@@ -6,6 +6,16 @@ import { AuthService } from '../services/AuthService';
import
{
AbstractEntity
}
from
'
./AbstractEntity
'
;
export
class
UserEntity
extends
AbstractEntity
implements
IUserEntity
{
private
_tokenHash
:
string
;
get
tokenHash
():
string
{
return
this
.
_tokenHash
;
}
set
tokenHash
(
value
:
string
)
{
this
.
_tokenHash
=
value
;
}
private
_token
:
string
;
get
token
():
string
{
...
...
@@ -73,6 +83,7 @@ export class UserEntity extends AbstractEntity implements IUserEntity {
this
.
_name
=
data
.
name
;
this
.
_privateKey
=
data
.
privateKey
;
this
.
_passwordHash
=
data
.
passwordHash
;
this
.
_tokenHash
=
data
.
tokenHash
;
this
.
_gitlabToken
=
data
.
gitlabToken
;
this
.
_token
=
data
.
token
;
this
.
_userAuthorizations
=
data
.
userAuthorizations
.
map
(
val
=>
UserRole
[
val
]);
...
...
@@ -94,6 +105,7 @@ export class UserEntity extends AbstractEntity implements IUserEntity {
token
:
this
.
token
,
name
:
this
.
name
,
passwordHash
:
this
.
passwordHash
,
tokenHash
:
this
.
tokenHash
,
privateKey
:
this
.
privateKey
,
gitlabToken
:
this
.
gitlabToken
,
userAuthorizations
:
this
.
userAuthorizations
,
...
...
src/interfaces/users/IUserBase.ts
View file @
ab3eb3df
export
interface
IUserBase
{
name
:
string
;
passwordHash
:
string
;
tokenHash
:
string
;
privateKey
:
string
;
token
?:
string
;
gitlabToken
?:
string
;
...
...
src/models/UserModelItem/UserModel.ts
View file @
ab3eb3df
...
...
@@ -8,7 +8,8 @@ import LoggerService from '../../services/LoggerService';
@
index
({
name
:
1
},
{
unique
:
true
})
export
class
UserModelItem
extends
Typegoose
implements
IUserSerialized
{
@
prop
({
required
:
true
})
public
name
:
string
;
@
prop
({
required
:
true
})
public
passwordHash
:
string
;
@
prop
({
required
:
false
})
public
passwordHash
:
string
;
@
prop
({
required
:
false
})
public
tokenHash
:
string
;
@
prop
({
required
:
true
})
public
userAuthorizations
:
Array
<
string
>
;
@
prop
({
required
:
true
})
public
privateKey
:
string
;
@
prop
()
public
gitlabToken
?:
string
;
...
...
src/routers/rest/AdminRouter.ts
View file @
ab3eb3df
...
...
@@ -42,6 +42,7 @@ export class AdminRouter extends AbstractRouter {
@
BodyParam
(
'
name
'
)
name
:
string
,
//
@
BodyParam
(
'
privateKey
'
)
privateKey
:
string
,
//
@
BodyParam
(
'
passwordHash
'
)
passwordHash
:
string
,
//
@
BodyParam
(
'
passwordHash
'
)
tokenHash
:
string
,
//
@
BodyParam
(
'
userAuthorizations
'
)
userAuthorizations
:
Array
<
string
>
,
//
@
BodyParam
(
'
gitlabToken
'
,
{
required
:
false
})
gitlabToken
:
string
,
//
):
void
{
...
...
@@ -49,6 +50,7 @@ export class AdminRouter extends AbstractRouter {
const
userData
:
IUserSerialized
=
{
name
,
passwordHash
,
tokenHash
,
privateKey
,
userAuthorizations
,
gitlabToken
,
...
...
src/routers/rest/LibRouter.ts
View file @
ab3eb3df
...
...
@@ -340,14 +340,36 @@ export class LibRouter extends AbstractRouter {
@
Post
(
'
/authorize/static
'
)
private
async
authorizeStatic
(
@
BodyParam
(
'
username
'
)
username
:
string
,
@
BodyParam
(
'
passwordHash
'
)
password
:
string
,
@
BodyParam
(
'
username
'
,
{
required
:
false
})
username
:
string
,
@
BodyParam
(
'
passwordHash
'
,
{
required
:
false
})
password
:
string
,
@
BodyParam
(
'
tokenHash
'
,
{
required
:
false
})
tokenHash
:
string
,
@
BodyParam
(
'
token
'
,
{
required
:
false
})
token
:
string
,
):
Promise
<
object
>
{
const
user
=
UserDAO
.
getUser
(
username
);
let
user
;
if
(
username
)
{
user
=
UserDAO
.
getUser
(
username
);
if
(
!
username
||
!
password
||
!
user
||
!
UserDAO
.
validateUser
(
username
,
password
))
{
if
(
!
password
||
!
user
||
!
UserDAO
.
validateUser
(
username
,
password
))
{
throw
new
UnauthorizedError
(
JSON
.
stringify
({
status
:
StatusProtocol
.
Failed
,
step
:
MessageProtocol
.
AuthenticateStatic
,
payload
:
{
reason
:
'
UNKOWN_LOGIN
'
},
}));
}
}
else
if
(
tokenHash
)
{
user
=
UserDAO
.
getUserByTokenHash
(
tokenHash
);
if
(
!
user
)
{
throw
new
UnauthorizedError
(
JSON
.
stringify
({
status
:
StatusProtocol
.
Failed
,
step
:
MessageProtocol
.
AuthenticateStatic
,
payload
:
{
reason
:
'
UNKOWN_LOGIN
'
},
}));
}
}
else
{
throw
new
UnauthorizedError
(
JSON
.
stringify
({
status
:
StatusProtocol
.
Failed
,
step
:
MessageProtocol
.
AuthenticateStatic
,
...
...
src/tests/routes/expiry-quiz.test.ts
View file @
ab3eb3df
...
...
@@ -28,6 +28,7 @@ class ExpiryQuizTestSuite {
LoginDAO
.
initUser
({
name
:
'
testuser
'
,
passwordHash
:
'
hash
'
,
tokenHash
:
'
hash
'
,
privateKey
:
'
mysecret
'
,
gitlabToken
:
''
,
userAuthorizations
:
[
UserRole
.
CreateExpiredQuiz
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment