-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from KPMP/develop
Release 0.1
- Loading branch information
Showing
10 changed files
with
223 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
language: java | ||
|
||
jdk: | ||
- oraclejdk8 | ||
|
||
install: true | ||
|
||
script: | ||
- ./gradlew build | ||
|
||
notifications: | ||
email: | ||
- [email protected] | ||
- [email protected] | ||
- [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
package org.kpmp.auth; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Random; | ||
|
||
@Component | ||
@Scope("singleton") | ||
public class SecurityConstants { | ||
public static final String SECRET = "TubuleGlomerulusNephron"; | ||
public static final long EXPIRATION_TIME = 28_800_000; // 8 hours | ||
|
||
private final byte[] secret; | ||
public static final long EXPIRATION_TIME = 28_800_000; | ||
|
||
public SecurityConstants() { | ||
Random random = new Random(); | ||
byte[] secret = new byte[32]; | ||
random.nextBytes(secret); | ||
this.secret = secret; | ||
} | ||
|
||
public byte[] getSecret() { | ||
return secret; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.kpmp.auth; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.JWTVerificationException; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.springframework.stereotype.Service; | ||
import users.User; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
import static com.auth0.jwt.algorithms.Algorithm.HMAC512; | ||
|
||
@Service | ||
public class TokenService { | ||
|
||
private SecurityConstants securityConstants; | ||
|
||
public TokenService(SecurityConstants securityConstants) { | ||
this.securityConstants = securityConstants; | ||
} | ||
|
||
public String buildTokenWithUser(User user) throws JsonProcessingException { | ||
return JWT.create().withSubject(user.getId()) | ||
.withExpiresAt(new Date(System.currentTimeMillis() + securityConstants.EXPIRATION_TIME)).withClaim("user", user.toJson()) | ||
.sign(HMAC512(securityConstants.getSecret())); | ||
} | ||
|
||
public DecodedJWT verifyToken(String token) { | ||
try { | ||
return JWT.require(Algorithm.HMAC512(securityConstants.getSecret())) | ||
.build() | ||
.verify(token); | ||
|
||
} catch (JWTVerificationException exception) { | ||
return null; | ||
} | ||
} | ||
|
||
public User getUserFromToken(DecodedJWT verifiedToken) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
try { | ||
return mapper.readValue(verifiedToken.getClaim("user").asString(), User.class); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.kpmp.auth; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SecurityConstantsTest { | ||
|
||
private SecurityConstants securityConstants; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
securityConstants = new SecurityConstants(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
securityConstants = null; | ||
} | ||
|
||
@Test | ||
public void testGetSecret() { | ||
assertEquals(securityConstants.getSecret().length, 32); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.kpmp.auth; | ||
|
||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import users.User; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class TokenServiceTest { | ||
|
||
private TokenService tokenService; | ||
@Mock | ||
private SecurityConstants securityConstants; | ||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
tokenService = new TokenService(securityConstants); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
tokenService = null; | ||
} | ||
|
||
@Test | ||
public void testBuildandVerifyToken() throws Exception { | ||
when(securityConstants.getSecret()).thenReturn("GiveMeTheInfinityStones".getBytes()); | ||
User user = new User(); | ||
user.setId("123"); | ||
user.setDisplayName("Thanos the Great"); | ||
user.setEmail("[email protected]"); | ||
user.setFirstName("Thanos"); | ||
user.setLastName("Smith"); | ||
String token = tokenService.buildTokenWithUser(user); | ||
assertNotNull(token); | ||
DecodedJWT verifiedToken = tokenService.verifyToken(token); | ||
assertNotNull(verifiedToken); | ||
User userFromJWT = tokenService.getUserFromToken(verifiedToken); | ||
assertEquals(userFromJWT.getDisplayName(), user.getDisplayName()); | ||
assertEquals(userFromJWT.getEmail(), user.getEmail()); | ||
assertEquals(userFromJWT.getFirstName(), user.getFirstName()); | ||
assertEquals(userFromJWT.getLastName(), user.getLastName()); | ||
assertEquals(verifiedToken.getSubject(), user.getId()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,4 +63,17 @@ public void testToString() { | |
", displayName: Space Oddity" + | ||
", email: [email protected]", testUser.toString()); | ||
} | ||
|
||
@Test | ||
public void testToJson() throws Exception{ | ||
testUser.setId("12345"); | ||
testUser.setDisplayName("Space Oddity"); | ||
testUser.setFirstName("Ziggy"); | ||
testUser.setLastName("Stardust"); | ||
testUser.setEmail("[email protected]"); | ||
assertEquals("{\"firstName\":\"Ziggy\"" + | ||
",\"lastName\":\"Stardust\"" + | ||
",\"displayName\":\"Space Oddity\"" + | ||
",\"email\":\"[email protected]\"}", testUser.toJson()); | ||
} | ||
} |