Skip to content

Commit 147e989

Browse files
committed
Refactored auth code
1 parent 4b63805 commit 147e989

File tree

14 files changed

+73
-90
lines changed

14 files changed

+73
-90
lines changed

config-layers/common/in/erail/common/FrameworkConfiguration.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ $class=in.erail.common.FrameworkConfiguration
33
$scope=GLOBAL
44

55
redisClient=/io/vertx/redis/RedisClient
6-
oAuth2Auth=/io/vertx/ext/auth/oauth2/OAuth2Auth

config-layers/common/in/erail/route/LoadUserFromAccessTokenRouteBuilder.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
$class=in.erail.route.LoadUserFromAccessTokenRouteBuillder
33

44
vertx=/io/vertx/core/Vertx
5+
authProvider=/io/vertx/ext/auth/jwt/JWTAuth
56
log=true
6-
userProvider=/in/erail/user/UserProvider

config-layers/common/in/erail/user/UserProvider.properties

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#/io/vertx/ext/auth/jwt/JWTAuth
2+
$class=io.vertx.reactivex.ext.auth.jwt.JWTAuth
3+
$instanceFactory=/in/erail/factory/MethodInstanceFactory
4+
$factory.class=io.vertx.reactivex.ext.auth.jwt.JWTAuth
5+
$factory.method.name=create
6+
$factory.param.values=/io/vertx/core/Vertx,/io/vertx/ext/auth/jwt/JWTAuthOptions
7+
$factory.param.type=io.vertx.reactivex.core.Vertx,io.vertx.ext.auth.jwt.JWTAuthOptions
8+
$factory.enable=true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#/io/vertx/ext/auth/jwt/JWTAuthOptions
2+
$class=io.vertx.ext.auth.jwt.JWTAuthOptions
3+
$instanceFactory=/in/erail/factory/ParameterConstructorInstanceFactory
4+
$constructor.param.values=jwtAuthOptionsConfig.json
5+
$constructor.param.type=io.vertx.core.json.JsonObject
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"pubSecKeys" : [
3+
{
4+
"algorithm" : "RS256",
5+
"publicKey" : "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr1MjI2WAw06VK8biQPC+0mVOkI4ZRt+327fxwrxyuu32AMPKznPCsKtRaI/Xma3+Yi09Dt99agkfUfw/rPr3N0Vcgzd1muhqZOe6vfWqmPCUUB263EkSa1GIx4pLZ3CfvAc3f24Y00CS+S0gjCIr7lwLelcL9Hu9eZg5sTEfzKB/3+yUJ875aYRGWXdM7DnNzODXKa9kc1EqWjwprX0UZwoo3OycgmYS+DyI/MHPcdWbilsjsw77ISFeBZ7OZ+hFJ/baABenAnsTwqbyvWlC6TS3GCXTeH10+0RGWvjJAZEPX0PEt626iMTqP61XEh8kpOROU/xmLWTKHz3EAcX+4QIDAQAB",
6+
"secretKey" : "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCvUyMjZYDDTpUrxuJA8L7SZU6QjhlG37fbt/HCvHK67fYAw8rOc8Kwq1Foj9eZrf5iLT0O331qCR9R/D+s+vc3RVyDN3Wa6Gpk57q99aqY8JRQHbrcSRJrUYjHiktncJ+8Bzd/bhjTQJL5LSCMIivuXAt6Vwv0e715mDmxMR/MoH/f7JQnzvlphEZZd0zsOc3M4Ncpr2RzUSpaPCmtfRRnCijc7JyCZhL4PIj8wc9x1ZuKWyOzDvshIV4Fns5n6EUn9toAF6cCexPCpvK9aULpNLcYJdN4fXT7REZa+MkBkQ9fQ8S3rbqIxOo/rVcSHySk5E5T/GYtZMofPcQBxf7hAgMBAAECggEAGOlXNNBXW2jvCSlZPKD4fCyo3SFTth5ToYShdWoRYz4sli87wdnw7+lnx9Oobs2qN4j2BAb9avOg36VX5txCBDh2zK40p64eb/f9MMCXXOPSLxAKXQKE+3q6VJ/x1uDJ0Y209Q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GlWOC419oXmR0yMmpjnGH3f2MFa41"
7+
}
8+
9+
]
10+
}
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package in.erail.common;
22

3-
import io.vertx.reactivex.ext.auth.oauth2.OAuth2Auth;
43
import io.vertx.reactivex.redis.RedisClient;
54
import java.util.Optional;
65

@@ -11,7 +10,6 @@
1110
public class FrameworkConfiguration {
1211

1312
private RedisClient mRedisClient;
14-
private OAuth2Auth mOAuth2Auth;
1513

1614
public RedisClient getRedisClient() {
1715
return mRedisClient;
@@ -21,20 +19,8 @@ public void setRedisClient(RedisClient pRedisClient) {
2119
this.mRedisClient = pRedisClient;
2220
}
2321

24-
public OAuth2Auth getOAuth2Auth() {
25-
return mOAuth2Auth;
26-
}
27-
28-
public void setOAuth2Auth(OAuth2Auth pOAuth2Auth) {
29-
this.mOAuth2Auth = pOAuth2Auth;
30-
}
31-
32-
3322
public boolean isRedisEnable(){
3423
return Optional.ofNullable(getRedisClient()).isPresent();
3524
}
3625

37-
public boolean isOAuth2AuthEnable(){
38-
return Optional.ofNullable(getOAuth2Auth()).isPresent();
39-
}
4026
}

src/main/java/in/erail/route/LoadUserFromAccessTokenRouteBuillder.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.google.common.base.Strings;
44
import com.google.common.net.HttpHeaders;
5-
import in.erail.user.UserProvider;
65
import io.vertx.core.json.JsonObject;
6+
import io.vertx.reactivex.ext.auth.AuthProvider;
77
import io.vertx.reactivex.ext.web.Router;
88
import io.vertx.reactivex.ext.web.RoutingContext;
99
import java.util.regex.Matcher;
@@ -16,7 +16,7 @@
1616
public class LoadUserFromAccessTokenRouteBuillder extends AbstractRouterBuilderImpl {
1717

1818
private final Pattern AUTH_TOKEN = Pattern.compile("^Bearer\\s(?<token>.*)");
19-
private UserProvider mUserProvider;
19+
private AuthProvider mAuthProvider;
2020

2121
@Override
2222
public Router getRouter(Router pRouter) {
@@ -29,14 +29,15 @@ public void handle(RoutingContext pRoutingContext) {
2929
if (pRoutingContext.user() == null) {
3030
String access_token = pRoutingContext.request().getHeader(HttpHeaders.AUTHORIZATION);
3131
if (!Strings.isNullOrEmpty(access_token)) {
32-
Matcher token = AUTH_TOKEN.matcher(access_token);
33-
if (token.find()) {
34-
JsonObject accessToken = new JsonObject().put("access_token", token.group("token"));
32+
Matcher tokenRegex = AUTH_TOKEN.matcher(access_token);
33+
if (tokenRegex.find()) {
34+
String token = tokenRegex.group("token");
35+
JsonObject authInfo = new JsonObject()
36+
.put("access_token", token)
37+
.put("token_type", "Bearer")
38+
.put("jwt", token);
3539
try {
36-
pRoutingContext
37-
.setUser(getUserProvider()
38-
.getUser(accessToken)
39-
.blockingGet());
40+
pRoutingContext.setUser(getAuthProvider().rxAuthenticate(authInfo).blockingGet());
4041
} catch (RuntimeException e) {
4142
getLog().error(e);
4243
pRoutingContext.fail(401);
@@ -50,12 +51,12 @@ public void handle(RoutingContext pRoutingContext) {
5051
pRoutingContext.next();
5152
}
5253

53-
public UserProvider getUserProvider() {
54-
return mUserProvider;
54+
public AuthProvider getAuthProvider() {
55+
return mAuthProvider;
5556
}
5657

57-
public void setUserProvider(UserProvider pUserProvider) {
58-
this.mUserProvider = pUserProvider;
58+
public void setAuthProvider(AuthProvider pAuthProvider) {
59+
this.mAuthProvider = pAuthProvider;
5960
}
6061

6162
}

src/main/java/in/erail/route/OpenAPI3RouteBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
*/
3838
public class OpenAPI3RouteBuilder extends AbstractRouterBuilderImpl {
3939

40-
private static final String AUTHORIZATION_PREFIX = "realm";
4140
private static final String FAIL_SUFFIX = ".fail";
4241
private RESTService[] mServices;
4342
private File mOpenAPI3File;
@@ -233,14 +232,14 @@ public Router getRouter(Router pRouter) {
233232
.forEach((service) -> {
234233
getLog().debug(() -> "Adding OpenAPI service handle:" + service.getOperationId());
235234
apiFactory.addHandlerByOperationId(service.getOperationId(), (routingContext) -> {
236-
if (isSecurityEnable()) {
235+
if (isSecurityEnable() && service.isSecure()) {
237236

238237
if (routingContext.user() == null) {
239238
routingContext.fail(401);
240239
return;
241240
}
242241

243-
routingContext.user().isAuthorized(AUTHORIZATION_PREFIX + ":" + service.getOperationId(), (event) -> {
242+
routingContext.user().isAuthorized(service.getAuthority(), (event) -> {
244243
boolean authSuccess = event.succeeded() ? event.result() : false;
245244
if (authSuccess) {
246245
process(routingContext, service.getServiceUniqueId());

src/main/java/in/erail/service/RESTService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package in.erail.service;
22

3-
43
import in.erail.model.RequestEvent;
54
import in.erail.model.ResponseEvent;
65
import io.reactivex.Maybe;
@@ -10,7 +9,14 @@
109
* @author vinay
1110
*/
1211
public interface RESTService {
12+
1313
String getOperationId();
14+
1415
String getServiceUniqueId();
16+
1517
Maybe<ResponseEvent> process(RequestEvent pRequest);
18+
19+
String getAuthority();
20+
21+
boolean isSecure();
1622
}

0 commit comments

Comments
 (0)