Skip to content

Commit

Permalink
#9 add start at a jwt support service
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed Aug 6, 2019
1 parent d9f1958 commit 2134be7
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 0 deletions.
93 changes: 93 additions & 0 deletions irodsext-jwt-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>jargon-irods-ext</artifactId>
<groupId>org.irods.jargon</groupId>
<version>4.3.1.0-SNAPSHOT</version>
</parent>
<artifactId>irodsext-jwt-service</artifactId>
<name>irodsext-jwt-service</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<description>Tools for managing jwts used by associated microservices</description>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>0</id>
<phase>validate</phase>
<configuration>
<tasks>
<delete
file="${basedir}/src/test/resources/testing.properties" />
<touch
file="${basedir}/src/test/resources/testing.properties"
mkdirs="true" />
<echo
file="${basedir}/src/test/resources/testing.properties"
append="true">
test.confirm=${jargon.test.confirm}
test.data.directory=${jargon.test.data.directory}
test.irods.admin=${jargon.test.irods.admin}
test.irods.admin.password=${jargon.test.irods.admin.password}
test.irods.user=${jargon.test.irods.user}
test.irods.password=${jargon.test.irods.password}
test.irods.resource=${jargon.test.irods.resource}
test2.irods.user=${jargon.test.irods.user2}
test2.irods.password=${jargon.test.irods.password2}
test2.irods.resource=${jargon.test.irods.resource2}
test3.irods.user=${jargon.test.irods.user3}
test3.irods.password=${jargon.test.irods.password3}
test3.irods.resource=${jargon.test.irods.resource3}
test.irods.host=${jargon.test.irods.host}
test.irods.port=${jargon.test.irods.port}
test.irods.zone=${jargon.test.irods.zone}
test.resource.group=${jargon.test.resource.group}
test.irods.userDN=${jargon.test.irods.userDN}
test.irods.scratch.subdir=${jargon.test.irods.scratch.subdir}
</echo>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</build>

</project>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
*
*/
package org.irods.jargon.irodsext.jwt;

import java.security.Key;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

/**
* @author Mike Conway - NIEHS
*
*/
public class JwtIssueServiceImpl {

public static final Logger log = LoggerFactory.getLogger(JwtIssueServiceImpl.class);

private final JwtServiceConfig jwtServiceConfig;
private final Key myKey;

/**
* Constructor with configs
*
* @param jwtServiceConfig {@link JwtServiceConfig}
*/
public JwtIssueServiceImpl(final JwtServiceConfig jwtServiceConfig) {
if (jwtServiceConfig == null) {
throw new IllegalArgumentException("null jwtServiceConfig");
}

this.jwtServiceConfig = jwtServiceConfig;
myKey = Keys.hmacShaKeyFor(jwtServiceConfig.getSecret().getBytes());
}

public String issueJwtToken(final String subject) {
log.info("issueJwtToken()");

if (subject == null || subject.isEmpty()) {
throw new IllegalArgumentException("null or empty subject");
}

String signedJwt = Jwts.builder().setSubject(subject).setIssuer(jwtServiceConfig.getIssuer())
.setIssuedAt(new Date()).signWith(myKey).compact();
return signedJwt;

}

public Jws<Claims> decodeJwtToken(final String token) {
log.info("decodeJwtToken()");

if (token == null || token.isEmpty()) {
throw new IllegalArgumentException("null or empty token");
}

Jws<Claims> claims = Jwts.parser().setSigningKey(myKey).parseClaimsJws(token);
return claims;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
*
*/
package org.irods.jargon.irodsext.jwt;

/**
* Basic configs for a service to issue and decode jwts used in iRODS
* microservices
*
* @author Mike Conway - NIEHS
*
*/
public class JwtServiceConfig {

/**
* Issuer typically in reverse dns name format, used as "iss" in the JWT
*/
private String issuer = "";
/**
* Secret used to sign tokens given the provided algo
*/
private String secret = "";
/**
* Signing algo used in JWT
*/
private String algo = "";

public String getIssuer() {
return issuer;
}

public void setIssuer(String issuer) {
this.issuer = issuer;
}

public String getSecret() {
return secret;
}

public void setSecret(String secret) {
this.secret = secret;
}

public String getAlgo() {
return algo;
}

public void setAlgo(String algo) {
this.algo = algo;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("JwtServiceConfig [issuer=").append(issuer).append(", algo=").append(algo).append("]");
return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

/**
* Utils and services to support use of JWT tokens in Jargon extensions and
* microservices
*
* @author Mike Conway - NIEHS
*
*/
package org.irods.jargon.irodsext.jwt;
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.irods.jargon.irodsext.jwt;

import org.junit.Assert;
import org.junit.Test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.WeakKeyException;

public class JwtIssueServiceImplTest {

@Test(expected = WeakKeyException.class)
public void testIssueJwtWeakKey() {
JwtServiceConfig config = new JwtServiceConfig();
config.setAlgo(SignatureAlgorithm.HS256.getValue());
config.setIssuer("test");
config.setSecret("thisisasecret");
JwtIssueServiceImpl jwtIssueServiceImpl = new JwtIssueServiceImpl(config);
jwtIssueServiceImpl.issueJwtToken("subject");
}

@Test
public void testIssueJwt() {
JwtServiceConfig config = new JwtServiceConfig();
config.setAlgo(SignatureAlgorithm.HS256.getValue());
config.setIssuer("test");
config.setSecret("thisisasecretthatisverysecretyouwillneverguessthiskey");
JwtIssueServiceImpl jwtIssueServiceImpl = new JwtIssueServiceImpl(config);
String jwt = jwtIssueServiceImpl.issueJwtToken("subject");
Assert.assertNotNull("no jwt issued", jwt);
}

@Test
public void testIssueAndDecodeJwt() {
JwtServiceConfig config = new JwtServiceConfig();
config.setAlgo(SignatureAlgorithm.HS256.getValue());
config.setIssuer("test");
config.setSecret("thisisasecretthatisverysecretyouwillneverguessthiskeyhurray");
JwtIssueServiceImpl jwtIssueServiceImpl = new JwtIssueServiceImpl(config);
String jwt = jwtIssueServiceImpl.issueJwtToken("subject");
Jws<Claims> actual = jwtIssueServiceImpl.decodeJwtToken(jwt);
Assert.assertNotNull("claims not returned", actual);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.irods.jargon.irodsext.jwt.unittest;

import org.irods.jargon.irodsext.jwt.JwtIssueServiceImplTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ JwtIssueServiceImplTest.class })
public class AllTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
*
*/
/**
* Suites and test utils
*
* @author Mike Conway - NIEHS
*
*/
package org.irods.jargon.irodsext.jwt.unittest;
11 changes: 11 additions & 0 deletions irodsext-jwt-service/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set root logger level to DEBUG and its only appender to A1.
#log4j.rootLogger=ERROR, A1
log4j.category.org.irods.jargon.core=INFO, A1
log4j.category.org.irods.jargon.datautils=INFO, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,6 @@
<module>irodsext-data-typer</module>
<module>emc-metalnx-core</module>
<module>emc-metalnx-services</module>
<module>irodsext-jwt-service</module>
</modules>
</project>

0 comments on commit 2134be7

Please sign in to comment.