-
-
Notifications
You must be signed in to change notification settings - Fork 39
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 #575 from kdhrubo/feature/574_auth
Feature/574 auth
- Loading branch information
Showing
29 changed files
with
300 additions
and
169 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
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
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,7 @@ | ||
app: | ||
databases: | ||
- name: ${DB_NAME:pgdb} | ||
type: POSTGRESQL | ||
url: jdbc:postgresql://localhost:5432/postgres | ||
username: postgres | ||
password: |
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,7 @@ | ||
app: | ||
databases: | ||
- name: ${DB_NAME:pgdb} | ||
type: POSTGRESQL | ||
url: jdbc:postgresql://localhost:5432/postgres | ||
username: postgres | ||
password: |
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
31 changes: 31 additions & 0 deletions
31
auth/src/main/java/com/homihq/db2rest/auth/AuthConfiguration.java
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,31 @@ | ||
package com.homihq.db2rest.auth; | ||
|
||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.JWTVerifier; | ||
import com.homihq.db2rest.auth.jwt.JwtAuthProvider; | ||
import com.homihq.db2rest.auth.jwt.JwtProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(prefix = "db2rest.auth", name="type" , havingValue = "jwt") | ||
public class AuthConfiguration { | ||
|
||
private final JwtProperties jwtProperties; | ||
|
||
@Bean | ||
public JwtAuthProvider jwtAuthProvider() { | ||
JWTVerifier jwtVerifier = | ||
JWT.require(jwtProperties.getAlgo()) | ||
.withIssuer(jwtProperties.getIssuers()) | ||
.build(); | ||
|
||
return new JwtAuthProvider(jwtVerifier); | ||
} | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
auth/src/main/java/com/homihq/db2rest/auth/basic/BasicAuthProvider.java
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,39 @@ | ||
package com.homihq.db2rest.auth.basic; | ||
|
||
import com.homihq.db2rest.auth.common.AuthDataProvider; | ||
import com.homihq.db2rest.auth.common.AuthProvider; | ||
import com.homihq.db2rest.auth.common.Subject; | ||
import com.homihq.db2rest.auth.common.User; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class BasicAuthProvider implements AuthProvider { | ||
|
||
private final AuthDataProvider authDataProvider; | ||
@Override | ||
public boolean canHandle(String authHeader) { | ||
return StringUtils.isNotBlank(authHeader) && authHeader.startsWith("Basic "); | ||
} | ||
|
||
@Override | ||
public Subject handle(String authHeader) { | ||
String base64Credentials = authHeader.substring("Basic ".length()); | ||
byte[] decodedCredentials = Base64.getDecoder().decode(base64Credentials); | ||
String credentials = new String(decodedCredentials, StandardCharsets.UTF_8); | ||
|
||
String[] parts = credentials.split(":"); | ||
String username = parts[0]; | ||
String password = parts[1]; | ||
|
||
User user = authDataProvider.validate(username, password); | ||
|
||
return new Subject(username, user.roles(), ""); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
auth/src/main/java/com/homihq/db2rest/auth/common/ApiResource.java
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,6 @@ | ||
package com.homihq.db2rest.auth.common; | ||
|
||
import java.util.List; | ||
|
||
public record ApiResource(String path, String method, List<String> roles) { | ||
} |
17 changes: 0 additions & 17 deletions
17
auth/src/main/java/com/homihq/db2rest/auth/common/AuthContext.java
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
auth/src/main/java/com/homihq/db2rest/auth/common/AuthDataProvider.java
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,20 @@ | ||
package com.homihq.db2rest.auth.common; | ||
|
||
import com.homihq.db2rest.auth.exception.AuthException; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.List; | ||
|
||
public interface AuthDataProvider { | ||
|
||
List<ApiResource> getApiResources(); | ||
List<User> getUsers(); | ||
|
||
default User validate(String username, String password) { | ||
return | ||
getUsers().stream() | ||
.filter(u -> StringUtils.equals(u.username(), username) | ||
&& StringUtils.equals(u.username(), password)).findFirst().orElseThrow(() -> | ||
new AuthException("Invalid username or password.")); | ||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
auth/src/main/java/com/homihq/db2rest/auth/common/AuthInfo.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
auth/src/main/java/com/homihq/db2rest/auth/common/AuthProvider.java
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,8 @@ | ||
package com.homihq.db2rest.auth.common; | ||
|
||
public interface AuthProvider { | ||
|
||
boolean canHandle(String authHeader); | ||
|
||
Subject handle(String authHeader); | ||
} |
5 changes: 5 additions & 0 deletions
5
auth/src/main/java/com/homihq/db2rest/auth/common/Subject.java
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,5 @@ | ||
package com.homihq.db2rest.auth.common; | ||
|
||
import java.util.List; | ||
|
||
public record Subject (String principal, List<String> roles, String requestedUri) { } |
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,6 @@ | ||
package com.homihq.db2rest.auth.common; | ||
|
||
import java.util.List; | ||
|
||
public record User(String username, String password, List<String> roles) { | ||
} |
21 changes: 21 additions & 0 deletions
21
auth/src/main/java/com/homihq/db2rest/auth/data/FileAuthDataProvider.java
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,21 @@ | ||
package com.homihq.db2rest.auth.data; | ||
|
||
import com.homihq.db2rest.auth.common.ApiResource; | ||
import com.homihq.db2rest.auth.common.AuthDataProvider; | ||
import com.homihq.db2rest.auth.common.User; | ||
|
||
import java.util.List; | ||
|
||
public class FileAuthDataProvider implements AuthDataProvider { | ||
|
||
|
||
@Override | ||
public List<ApiResource> getApiResources() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<User> getUsers() { | ||
return null; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
auth/src/main/java/com/homihq/db2rest/auth/exception/AuthException.java
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,9 @@ | ||
package com.homihq.db2rest.auth.exception; | ||
|
||
import org.springframework.core.NestedRuntimeException; | ||
|
||
public class AuthException extends NestedRuntimeException { | ||
public AuthException(String msg) { | ||
super(msg); | ||
} | ||
} |
Oops, something went wrong.