Skip to content

Commit

Permalink
Feat/#41 exception handling (#380)
Browse files Browse the repository at this point in the history
* Feat/#41 exception handling (#377) merge from fork(most part is done, will keep working on the branch instead of fork.)

* -added an error message in status.yml for validating response content
-now deserialize content string before do validation
-fixed a issue when matching a given uri with schema, now remove query parameters from uri
-changed some testing content

* -changed status code and moved it to swagger-validator errors position.

* -changed OauthHelper.getToken return type to com.networknt.monad.Result so that it can keep Status info when fail
-refactored token handler so that it can handle error situation.

* -changed Http2Client only get token when success response. and log error when fail.

* -added a Jwt model to be used in multiple places to store jwt info.
-added more error scenarios when get token from auth server
-refactored TokenHandler
-added Error Handling for SAMLTokenHandler

* -instead of throwing exceptions, all methods that needs to call OauthHelper.populateCCToken have changed return type to "Result"
-to avoid modifying class members, move checkCCExpired() method from Http2Client to OauthHelper, and make it public static so that it can be use in multiple modules
-TokenHandler in light-router will also use OauthHelper to populate client credential token
-added ClientException check in ExceptionHandler
-added Status field in ClientException

* -changed synchronized(Http2Client.class) to synchronized(OauthHelper.class) since it's moved to OauthHelper now

* -created a Enum "ContentType" in http-string
-in OauthHelper, whenever needs to pass auth server's response to the client, now it will escape based on the type that the server returns.
-related pull request: #380

* Removing some unused commits

* Fixing bad merge

* -fixed duplicate escape response

* -added class level comments
  • Loading branch information
BalloonWen authored and stevehu committed Feb 11, 2019
1 parent e778912 commit 45c80bb
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 246 deletions.
5 changes: 4 additions & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<groupId>com.networknt</groupId>
<artifactId>status</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>monad-result</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -85,5 +89,4 @@
<scope>test</scope>
</dependency>
</dependencies>

</project>
161 changes: 38 additions & 123 deletions client/src/main/java/com/networknt/client/Http2Client.java

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions client/src/main/java/com/networknt/client/oauth/Jwt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.networknt.client.oauth;

import com.networknt.config.Config;

import java.util.Map;

/**
* a model class represents a JWT mostly for caching usage so that we don't need to decrypt jwt string to get info.
* it will load config from client.yml/oauth/token
*/
public class Jwt {
private String jwt; // the cached jwt token for client credentials grant type
private long expire; // jwt expire time in millisecond so that we don't need to parse the jwt.
private volatile boolean renewing = false;
private volatile long expiredRetryTimeout;
private volatile long earlyRetryTimeout;

private static long tokenRenewBeforeExpired;
private static long expiredRefreshRetryDelay;
private static long earlyRefreshRetryDelay;

static final String OAUTH = "oauth";
static final String TOKEN = "token";
static final String TOKEN_RENEW_BEFORE_EXPIRED = "tokenRenewBeforeExpired";
static final String EXPIRED_REFRESH_RETRY_DELAY = "expiredRefreshRetryDelay";
static final String EARLY_REFRESH_RETRY_DELAY = "earlyRefreshRetryDelay";
public static final String CLIENT_CONFIG_NAME = "client";

public Jwt() {
Map<String, Object> clientConfig = Config.getInstance().getJsonMapConfig(CLIENT_CONFIG_NAME);
if(clientConfig != null) {
Map<String, Object> oauthConfig = (Map<String, Object>)clientConfig.get(OAUTH);
if(oauthConfig != null) {
Map<String, Object> tokenConfig = (Map<String, Object>)oauthConfig.get(TOKEN);
tokenRenewBeforeExpired = (Integer) tokenConfig.get(TOKEN_RENEW_BEFORE_EXPIRED);
expiredRefreshRetryDelay = (Integer)tokenConfig.get(EXPIRED_REFRESH_RETRY_DELAY);
earlyRefreshRetryDelay = (Integer)tokenConfig.get(EARLY_REFRESH_RETRY_DELAY);
}
}
}

public String getJwt() {
return jwt;
}

public void setJwt(String jwt) {
this.jwt = jwt;
}

public long getExpire() {
return expire;
}

public void setExpire(long expire) {
this.expire = expire;
}

public boolean isRenewing() {
return renewing;
}

public void setRenewing(boolean renewing) {
this.renewing = renewing;
}

public long getExpiredRetryTimeout() {
return expiredRetryTimeout;
}

public void setExpiredRetryTimeout(long expiredRetryTimeout) {
this.expiredRetryTimeout = expiredRetryTimeout;
}

public long getEarlyRetryTimeout() {
return earlyRetryTimeout;
}

public void setEarlyRetryTimeout(long earlyRetryTimeout) {
this.earlyRetryTimeout = earlyRetryTimeout;
}

public static long getTokenRenewBeforeExpired() {
return tokenRenewBeforeExpired;
}

public static void setTokenRenewBeforeExpired(long tokenRenewBeforeExpired) {
Jwt.tokenRenewBeforeExpired = tokenRenewBeforeExpired;
}

public static long getExpiredRefreshRetryDelay() {
return expiredRefreshRetryDelay;
}

public static void setExpiredRefreshRetryDelay(long expiredRefreshRetryDelay) {
Jwt.expiredRefreshRetryDelay = expiredRefreshRetryDelay;
}

public static long getEarlyRefreshRetryDelay() {
return earlyRefreshRetryDelay;
}

public static void setEarlyRefreshRetryDelay(long earlyRefreshRetryDelay) {
Jwt.earlyRefreshRetryDelay = earlyRefreshRetryDelay;
}
}
Loading

0 comments on commit 45c80bb

Please sign in to comment.