Skip to content

Commit

Permalink
Cleanup code in akamai module (frankframework#2317)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored Oct 12, 2021
1 parent 49a4264 commit b4d4186
Show file tree
Hide file tree
Showing 68 changed files with 831 additions and 507 deletions.
5 changes: 5 additions & 0 deletions akamai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<groupId>org.ibissource</groupId>
<artifactId>ibis-adapterframework-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2021 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nl.nn.adapterframework.extensions.akamai;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import lombok.Getter;
import nl.nn.adapterframework.stream.Message;
import nl.nn.adapterframework.util.Misc;

/**
* An enum of the hash algorithms. Currently supported hashes include MD5; SHA1; SHA256
*
* The string representation matches the java {@link java.security.MessageDigest#getInstance(String)} canonical names.
*
* @author Niels Meijer
*/
public enum HashAlgorithm {
MD5("MD5"), SHA1("SHA-1"), SHA256("SHA-256");

/**
* Algorithm name as defined in {@link java.security.MessageDigest#getInstance(String)}
*/
private @Getter final String algorithm;

private HashAlgorithm(final String algorithm) {
this.algorithm = algorithm;
}

public String computeHash(Message file) throws IOException {
byte[] fileBytes = file.asByteArray();
if (fileBytes == null) {
throw new IllegalStateException("unable to compute hash over null message");
}

byte[] checksum = computeHash(fileBytes, this);
if(checksum != null) {
return Misc.asHex(checksum);
}

throw new IllegalStateException("error computing checksum");
}

/**
* Computes the hash of a given InputStream. This is a wrapper over the MessageDigest crypto functions.
*
* @param srcBytes to generate a hash over
* @param hashAlgorithm the Algorithm to use to compute the hash
* @return a byte[] representation of the hash. If the InputStream is a null object
* then null will be returned. If the InputStream is empty an empty byte[] {} will be returned.
*/
private static byte[] computeHash(byte[] srcBytes, HashAlgorithm hashAlgorithm) {
try {
MessageDigest digest = MessageDigest.getInstance(hashAlgorithm.getAlgorithm());

return digest.digest(srcBytes);
} catch (NoSuchAlgorithmException e) {
//no-op. This will never happen since we are using an enum to limit the hash algorithms
throw new IllegalArgumentException("This should never happen! We are using an enum!", e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Random;

import nl.nn.adapterframework.extensions.akamai.NetStorageUtils.KeyedHashAlgorithm;
import nl.nn.adapterframework.util.CredentialFactory;


/**
Expand Down Expand Up @@ -70,19 +71,17 @@ public KeyedHashAlgorithm getAlgorithm() {
private URI uri;
private String nonce;
private String accessToken;
private NetStorageAction action;
private SignType signVersion;
private SignType signType;

/**
* Primary invocation for an API communication. This constructor is used for convenience when not uploading content
*
* @param uri the url to interact with (eg: http://example.akamaihd.net/254462 )
* @param nonce provisioned nonce from the Luna portal
* @param accessToken the associated accessToken generated by the portal
* @param action the set of action parameters to be sent in the API request
*/
public NetStorageCmsSigner(URI uri, String nonce, String accessToken, NetStorageAction action) {
this(uri, nonce, accessToken, action, SignType.HMACSHA256);
public NetStorageCmsSigner(URI uri, String nonce, String accessToken) {
this(uri, nonce, accessToken, SignType.HMACSHA256);
}

/**
Expand All @@ -91,32 +90,26 @@ public NetStorageCmsSigner(URI uri, String nonce, String accessToken, NetStorage
* @param uri the url to interact with (eg: http://example.akamaihd.net/254462 )
* @param nonce provisioned nonce from the Luna portal
* @param accessToken the associated accessToken generated by the portal
* @param action the set of action parameters to be sent in the API request
* @param signVersion the base64 encoded signature algorithm
* @param signType the base64 encoded signature algorithm
*/
public NetStorageCmsSigner(URI uri, String nonce, String accessToken, NetStorageAction action, SignType signVersion) {
public NetStorageCmsSigner(URI uri, String nonce, String accessToken, SignType signType) {
this.uri = uri;
this.nonce = nonce;
this.accessToken = accessToken;
this.action = action;
this.signVersion = signVersion;
}

public SignType getSignVersion() {
return signVersion;
this.signType = signType;
}

public void setSignVersion(SignType signVersion) {
this.signVersion = signVersion;
public NetStorageCmsSigner(URI uri, CredentialFactory accessTokenCf, SignType signType) {
this(uri, accessTokenCf.getUsername(), accessTokenCf.getPassword(), signType);
}

/**
* Computes the value for the the X-Akamai-ACS-Action: header. This is an url query-string encoded separated
* list of parameters in the form of name=value&name2=value2.
*
* @return an url encoded query string of name-value pairs from the {@link nl.nn.adapterframework.extensions.akamai.NetStorageAction}
* @param action the set of action parameters to be sent in the API request
* @return an url encoded query string of name-value pairs from the {@link nl.nn.adapterframework.extensions.akamai.NetStorageRequest}
*/
protected String getActionHeaderValue() {
protected String getActionHeaderValue(NetStorageRequest action) {
return action.compileHeader();
}

Expand All @@ -132,7 +125,7 @@ protected String getAuthDataHeaderValue() {

return String.format(
"%d, 0.0.0.0, 0.0.0.0, %d, %d, %s",
this.getSignVersion().getValue(),
signType.getValue(),
currentTime.getTime()/1000,
rand,
nonce);
Expand All @@ -143,7 +136,7 @@ protected String getAuthDataHeaderValue() {
* encoded representation of the hash as required by the spec. The api server will compute this same hash to validate
* the request
*
* @param action action header values {@link #getActionHeaderValue()}
* @param action action header values {@link #getActionHeaderValue(NetStorageRequest)}
* @param authData data header values {@link #getAuthDataHeaderValue()}
* @return a base64 encoded return string
*/
Expand All @@ -154,18 +147,19 @@ protected String getAuthSignHeaderValue(String action, String authData) {
uri.getPath(),
NetStorageCmsSigner.ACTION_HEADER.toLowerCase(),
action);
byte[] hash = NetStorageUtils.computeKeyedHash(signData.getBytes(), accessToken, this.getSignVersion().getAlgorithm());
byte[] hash = NetStorageUtils.computeKeyedHash(signData.getBytes(), accessToken, signType.getAlgorithm());

return NetStorageUtils.encodeBase64(hash);
}

/**
* Assembles the HTTP Headers necessary for API communication
* @param netStorageAction the set of action parameters to be sent in the API request
* @return Map of name-value pairs representing HTTP Headers and values.
*/
public Map<String, String> computeHeaders() {
final Map<String, String> headers = new HashMap<String, String>(3);
final String action = getActionHeaderValue();
public Map<String, String> computeHeaders(NetStorageRequest netStorageAction) {
final Map<String, String> headers = new HashMap<>(3);
final String action = getActionHeaderValue(netStorageAction);
final String authData = getAuthDataHeaderValue();
final String authSign = getAuthSignHeaderValue(action, authData);

Expand Down
Loading

0 comments on commit b4d4186

Please sign in to comment.