Implemented token-based API authentication.

Replaced the ApiKey concept with ephemeral tokens. Users and apps obtain a
token by authenticating the user credentials (to be implemented). The service
then generates a temporary token that is stored by the client and sent with
every request using the `Authorization-Token` header. The server verifies this
token to recognize and authenticate the request. With an authenticated user,
the server can use the user's role to authorize requests.

This implementation uses JSR 250 SecurityContext and security annotations.
This commit is contained in:
Jonathan Bernard
2015-03-02 21:20:25 -06:00
parent 38e0432c1e
commit 83a0f7275c
9 changed files with 141 additions and 16 deletions

View File

@@ -1,7 +0,0 @@
package com.jdbernard.nlsongs.model
public class ApiKey implements Serializable {
String key
String description
}

View File

@@ -0,0 +1,3 @@
package com.jdbernard.nlsongs.model;
public enum Role { admin, user }

View File

@@ -0,0 +1,17 @@
package com.jdbernard.nlsongs.model
public class Token implements Serializable {
String token
User user
Date expires
@Override
public boolean equals(Object thatObj) {
if (thatObj == null) return false
if (!(thatObj instanceof Token)) return false
Token that = (Token) thatObj
return (this.token == that?.token) }
}

View File

@@ -1,8 +1,17 @@
package com.jdbernard.nlsongs.model
import com.lambdaworks.crypto.SCryptUtil
public class User {
int id
String username
String pwd
Role role
public void setPwd(String pwd) {
this.pwd = SCryptUtil.scrypt(pwd, 16384, 16, 1) }
public boolean checkPwd(String givenPwd) {
return SCryptUtil.check(this.pwd, givenPwd) }
}