Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ Integration as simple as adding following snippet to your pom.xml if you use Mav
<dependency>
<groupId>com.sequencing</groupId>
<artifactId>oauth2-core</artifactId>
<version>1.2</version>
<version>1.9</version>
</dependency>
```

or following line to the "dependencies" section of your build.gradle

```
compile 'com.sequencing:oauth2-core:1.2'
compile 'com.sequencing:oauth2-core:1.9'
```

Resources
Expand Down
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.sequencing</groupId>
<artifactId>oauth2-core</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<version>1.9</version>

<name>Sequencing oAuth API</name>
<description>Module provides API access to sequencing.com backend</description>
Expand Down Expand Up @@ -35,10 +35,10 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.6</java.version>
<java.version>1.7</java.version>
<httpclient.version>4.4.1</httpclient.version>
<gson.version>2.5</gson.version>
<slf4j.version>1.7.12</slf4j.version>
<slf4j.version>1.7.18</slf4j.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -133,6 +133,19 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public class AuthenticationParameters implements Serializable
public static final String DEFAULT_TOKEN_URI = "https://sequencing.com/oauth2/token";
public static final String DEFAULT_API_URI = "https://api.sequencing.com";
public static final String DEFAULT_RESPONSE_TYPE = "code";
public static final String DEFAULT_SCOPE = "demo";
public static final String DEFAULT_SCOPE = "demo,external";
public static final String DEFAULT_GRANT_TYPE = "authorization_code";
public static final String DEFAULT_GRANT_TYPE_REFRESH = "refresh_token";
public static final String DEFAULT_MOBILE_MODE = "0";

private static final Logger log = LoggerFactory.getLogger(AuthenticationParameters.class);

Expand Down Expand Up @@ -89,6 +90,11 @@ public class AuthenticationParameters implements Serializable
* token.
*/
private final String grantTypeRefreshToken;

/**
* Sequencing login page mode. 1 - mobile mode, 0 - web mode.
*/
private final String mobileMode;

private AuthenticationParameters(ConfigurationBuilder builder) {
oAuthAuthorizationUri = builder.oAuthAuthorizationUri;
Expand All @@ -102,6 +108,8 @@ private AuthenticationParameters(ConfigurationBuilder builder) {
scope = builder.scope;
grantType = builder.grantType;
grantTypeRefreshToken = builder.grantTypeRefreshToken;
mobileMode = builder.mobileMode;

}

public static class ConfigurationBuilder {
Expand All @@ -117,6 +125,7 @@ public static class ConfigurationBuilder {
private String scope;
private String grantType;
private String grantTypeRefreshToken;
private String mobileMode;

public ConfigurationBuilder()
{
Expand All @@ -128,6 +137,7 @@ public ConfigurationBuilder()
.withScope(DEFAULT_SCOPE)
.withGrantType(DEFAULT_GRANT_TYPE)
.withGrantTypeRefreshToken(DEFAULT_GRANT_TYPE_REFRESH)
.withMobileMode(DEFAULT_MOBILE_MODE)
.withState(nextState());
}

Expand Down Expand Up @@ -185,6 +195,11 @@ public ConfigurationBuilder withGrantTypeRefreshToken(String grantTypeRefreshTok
this.grantTypeRefreshToken = grantTypeRefreshToken;
return this;
}

public ConfigurationBuilder withMobileMode(String mobileMode) {
this.mobileMode = mobileMode;
return this;
}

public AuthenticationParameters build() {
return new AuthenticationParameters(this);
Expand Down Expand Up @@ -255,4 +270,8 @@ public String getGrantType() {
public String getGrantTypeRefreshToken() {
return grantTypeRefreshToken;
}

public String getMobileMode() {
return mobileMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public String getOwnFiles() throws NonAuthorizedException
return getFilesByType("uploaded");
}

@Override
public String getFiles() throws NonAuthorizedException
{
return getFilesByType("all");
}

/**
* Returns files depending on file type
*/
Expand All @@ -35,7 +41,7 @@ private String getFilesByType(String fileType) throws NonAuthorizedException
throw new NonAuthorizedException();
}

String uri = String.format("%s/DataSourceList?%s=true&shared=true",
String uri = String.format("%s/DataSourceList?%s=true",
client.getAuthenticationParameters().getApiUri(), fileType);

return HttpHelper.doOauthSecureGet(uri, client.getToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -23,7 +21,7 @@
*/
public class DefaultSequencingOAuth2Client implements SequencingOAuth2Client, Serializable
{
private static final long serialVersionUID = 367801346184616920L;
private static final long serialVersionUID = -1085956585099205527L;
private AuthenticationParameters parameters;
private volatile Token token;

Expand Down Expand Up @@ -83,9 +81,9 @@ public class DefaultSequencingOAuth2Client implements SequencingOAuth2Client, Se
private static final String ATTR_EXPRIES_IN = "expires_in";

/**
* Executor that handles token refresh
* Attribute for value mobile mode
*/
private ScheduledExecutorService tokenUpdateExecutor = Executors.newSingleThreadScheduledExecutor();
private static final String ATTR_MOBILE_MODE = "mobile";

public DefaultSequencingOAuth2Client(AuthenticationParameters parameters){
this.parameters = parameters;
Expand All @@ -99,6 +97,7 @@ public Map<String, String> getHttpParametersForRedirect() {
attribures.put(ATTR_STATE, parameters.getState());
attribures.put(ATTR_CLIENT_ID, parameters.getClientId());
attribures.put(ATTR_SCOPE, parameters.getScope());
attribures.put(ATTR_MOBILE_MODE, parameters.getMobileMode());
return attribures;
}

Expand Down Expand Up @@ -134,9 +133,7 @@ public Token authorize(String responseCode, String responseState) throws Illegal
String refreshToken = JsonHelper.getField(result, ATTR_REFRESH_TOKEN);
long timelife = Long.parseLong(JsonHelper.getField(result, ATTR_EXPRIES_IN));

token = new Token(accessToken, refreshToken, timelife);

runRefreshTokenExecutor();
token = new Token(accessToken, refreshToken, timelife, new Date());

return token;
}
Expand All @@ -154,6 +151,14 @@ public AuthenticationParameters getAuthenticationParameters() {

@Override
public Token getToken() {
if(System.currentTimeMillis() >= (token.getLastRefreshDate().getTime() + (token.getLifeTime() * 1000) - 30000) && isAuthorized()){
try {
refreshToken();
} catch (BasicAuthenticationFailedException e) {
log.debug("Error occured during refresh token", e.getMessage());
}
}

return token;
}

Expand All @@ -176,26 +181,8 @@ protected void refreshToken() throws BasicAuthenticationFailedException
String accessToken = JsonHelper.getField(result, ATTR_ACCESS_TOKEN);
long timelife = Long.parseLong(JsonHelper.getField(result, ATTR_EXPRIES_IN));

token = new Token(accessToken, token.getRefreshToken(), timelife);
log.debug("Token has been refreshed. New token value " + token.getAccessToken());
}

/**
* Runs executor for refreshing token
*/
private void runRefreshTokenExecutor() {
tokenUpdateExecutor.scheduleWithFixedDelay(new TokenRefreshTask(), 0, token.getLifeTime() - 60, TimeUnit.SECONDS);
}

class TokenRefreshTask implements Runnable
{
public void run() {
try {
refreshToken();
} catch (BasicAuthenticationFailedException e) {
log.debug("Error occured during refresh token", e.getMessage());
}
}
token = new Token(accessToken, token.getRefreshToken(), timelife, new Date());
log.info("Token has been refreshed. New token value " + token.getAccessToken());
}

private List<String> getAttributesForRedirectAsList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public interface SequencingFileMetadataApi {
* @return String json of file content
*/
public String getOwnFiles() throws NonAuthorizedException;

/**
* Returns all files from sequencing.com
* @return String json of file content
*/
public String getFiles() throws NonAuthorizedException;
}
13 changes: 12 additions & 1 deletion src/main/java/com/sequencing/oauth/core/Token.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sequencing.oauth.core;

import java.io.Serializable;
import java.util.Date;

/**
* Class that defines token attributes needed for making data
Expand All @@ -24,11 +25,17 @@ public class Token implements Serializable
* Access token lifetime
*/
private long lifetime = 0;

/**
* Last date of token refreshing
*/
private Date lastRefreshDate;

public Token(String accessToken, String refreshToken, long lifetime) {
public Token(String accessToken, String refreshToken, long lifetime, Date lastRefreshDate) {
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.lifetime = lifetime;
this.lastRefreshDate = lastRefreshDate;
}

public String getAccessToken() {
Expand All @@ -42,4 +49,8 @@ public String getRefreshToken() {
public long getLifeTime() {
return lifetime;
}

public Date getLastRefreshDate() {
return lastRefreshDate;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/sequencing/oauth/helper/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Iterator;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -49,4 +50,20 @@ public static String[] parseJsonArrayToStringArray(JsonArray jsonArray){
list.toArray(resultStringArray);
return resultStringArray;
}

/**
* Convert json to java object
*/
public static <T> T convertToJavaObject(String json, Class<T> classOf){
Gson gson = new Gson();
T object = gson.fromJson(json, classOf);
return object;
}

/**
* Convert java object to json format
*/
public static <T> String convertToJson(T object){
return new Gson().toJson(object);
}
}