|
2 | 2 |
|
3 | 3 | import com.google.common.base.Strings; |
4 | 4 | import com.google.common.net.HttpHeaders; |
| 5 | +import in.erail.user.UserProvider; |
5 | 6 | import io.vertx.core.json.JsonObject; |
6 | | -import io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl; |
7 | | -import io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl; |
8 | | -import io.vertx.reactivex.ext.auth.oauth2.AccessToken; |
9 | | -import io.vertx.reactivex.ext.auth.oauth2.OAuth2Auth; |
10 | 7 | import io.vertx.reactivex.ext.web.Router; |
11 | 8 | import io.vertx.reactivex.ext.web.RoutingContext; |
| 9 | +import java.util.regex.Matcher; |
| 10 | +import java.util.regex.Pattern; |
12 | 11 |
|
13 | 12 | /** |
14 | 13 | * |
15 | 14 | * @author vinay |
16 | 15 | */ |
17 | 16 | public class LoadUserFromAccessTokenRouteBuillder extends AbstractRouterBuilderImpl { |
18 | 17 |
|
19 | | - private OAuth2Auth mOAuth2Auth; |
20 | | - |
21 | | - public OAuth2Auth getOAuth2Auth() { |
22 | | - return mOAuth2Auth; |
23 | | - } |
24 | | - |
25 | | - public void setOAuth2Auth(OAuth2Auth pOAuth2Auth) { |
26 | | - this.mOAuth2Auth = pOAuth2Auth; |
27 | | - } |
| 18 | + private Pattern AUTH_TOKEN = Pattern.compile("^Bearer\\s(?<token>.*)"); |
| 19 | + private UserProvider mUserProvider; |
28 | 20 |
|
29 | 21 | @Override |
30 | 22 | public Router getRouter(Router pRouter) { |
31 | 23 | pRouter.route().handler(this::handle); |
32 | 24 | return pRouter; |
33 | | - |
34 | 25 | } |
35 | 26 |
|
36 | 27 | public void handle(RoutingContext pRoutingContext) { |
37 | | - |
| 28 | + |
38 | 29 | if (pRoutingContext.user() == null) { |
39 | 30 | String access_token = pRoutingContext.request().getHeader(HttpHeaders.AUTHORIZATION); |
40 | 31 | if (!Strings.isNullOrEmpty(access_token)) { |
41 | | - OAuth2AuthProviderImpl provider = (OAuth2AuthProviderImpl) getOAuth2Auth().getDelegate(); |
42 | | - JsonObject accessToken = new JsonObject().put("access_token", access_token.split(" ")[1]); |
43 | | - try { |
44 | | - OAuth2TokenImpl token = new OAuth2TokenImpl(provider, accessToken); |
45 | | - pRoutingContext.setUser(new AccessToken(token)); |
46 | | - } catch (RuntimeException e) { |
47 | | - getLog().error(e); |
48 | | - pRoutingContext.fail(401); |
49 | | - return; |
| 32 | + Matcher token = AUTH_TOKEN.matcher(access_token); |
| 33 | + if (token.find()) { |
| 34 | + JsonObject accessToken = new JsonObject().put("access_token", token.group("token")); |
| 35 | + try { |
| 36 | + pRoutingContext |
| 37 | + .setUser(getUserProvider() |
| 38 | + .getUser(accessToken) |
| 39 | + .blockingGet()); |
| 40 | + } catch (RuntimeException e) { |
| 41 | + getLog().error(e); |
| 42 | + pRoutingContext.fail(401); |
| 43 | + return; |
| 44 | + } |
| 45 | + } else { |
| 46 | + getLog().warn(() -> "Invalid Auth Header:" + access_token); |
50 | 47 | } |
51 | 48 | } |
52 | 49 | } |
53 | 50 | pRoutingContext.next(); |
54 | 51 | } |
55 | 52 |
|
| 53 | + public UserProvider getUserProvider() { |
| 54 | + return mUserProvider; |
| 55 | + } |
| 56 | + |
| 57 | + public void setUserProvider(UserProvider pUserProvider) { |
| 58 | + this.mUserProvider = pUserProvider; |
| 59 | + } |
| 60 | + |
56 | 61 | } |
0 commit comments