-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Authorization to use Open API
Removing basic authentication in favor of access token, client token, client secret combination. For information on how to upgrade see https://developer.akamai.com/introduction/Prov_Creds.html and the Readme. Various other fixes * Fixed issue with PurgeStatus init - type of some fields didn't match type in json and cannot set a primative to be null. * Upgrading groovy version * Removing unessary public and return statments and semicolons. Class.class is not needed. * Ditching defunct gmaven for the eclipse groovy plugin. * Preparing version 2 * Use HTTP Status code constant for clarity of meaning
- Loading branch information
Showing
13 changed files
with
373 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.velir.aem.akamai.ccu | ||
|
||
import groovy.transform.CompileStatic | ||
|
||
/** | ||
* Timestamp - Date Formatter | ||
* | ||
* @author Kai Rasmussen | ||
*/ | ||
@CompileStatic | ||
class Timestamp { | ||
private static final String CCU_FORMAT = "yyyyMMdd'T'HH:mm:ssZ" | ||
public static final String UTC = "UTC" | ||
public static final TimeZone UTCTZ = TimeZone.getTimeZone(UTC) | ||
|
||
static String getTimestamp(Date date){ | ||
date.format(CCU_FORMAT, UTCTZ) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/groovy/com/velir/aem/akamai/ccu/auth/Authorization.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.velir.aem.akamai.ccu.auth | ||
|
||
import com.velir.aem.akamai.ccu.Timestamp | ||
import com.velir.aem.akamai.ccu.impl.CcuManagerImpl | ||
import groovy.transform.builder.Builder | ||
import groovyx.net.http.Method | ||
|
||
import static java.util.UUID.randomUUID | ||
|
||
/** | ||
* Authorization - | ||
* | ||
* @author Kai Rasmussen | ||
*/ | ||
@Builder | ||
class Authorization { | ||
CcuManagerImpl.Credentials credentials | ||
String path, rootCcuUrl | ||
HashMap body, headers | ||
Method method | ||
|
||
String getAuthorization(){ | ||
String timeStamp = use(Timestamp){ new Date().timestamp } | ||
String nonce = randomUUID().toString() | ||
String unsignedAuth = "EG1-HMAC-SHA256 client_token=${credentials.clientToken};access_token=${credentials.accessToken};timestamp=${timeStamp};nonce=${nonce};" | ||
String signedAuth = signAuth(path, unsignedAuth, timeStamp, body, method, headers) | ||
"${unsignedAuth}signature=${signedAuth}" | ||
} | ||
|
||
private String signAuth(String path, String auth, String timestamp, HashMap body, Method method, HashMap headers) { | ||
Signature sigBuilder = Signature.builder() | ||
.secret(credentials.clientSecret).scheme("https").path(path) | ||
.timestamp(timestamp).host(rootCcuUrl.replaceFirst("https://", "")) | ||
.requestHeaders(headers).postBody(body).method(method).auth(auth) | ||
.build() | ||
sigBuilder.signature | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/groovy/com/velir/aem/akamai/ccu/auth/Signature.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.velir.aem.akamai.ccu.auth | ||
|
||
import groovy.json.JsonOutput | ||
import groovy.transform.builder.Builder | ||
import groovyx.net.http.Method | ||
|
||
import javax.crypto.Mac | ||
import javax.crypto.spec.SecretKeySpec | ||
import java.security.MessageDigest | ||
|
||
import static groovyx.net.http.Method.POST | ||
import static javax.crypto.Mac.getInstance | ||
import static org.apache.commons.codec.binary.Base64.encodeBase64String | ||
import static org.apache.commons.lang.StringUtils.EMPTY | ||
/** | ||
* AuthorizationBuilder - Responsible for translating a request into a signature | ||
* | ||
* @author Kai Rasmussen | ||
*/ | ||
|
||
@Builder | ||
class Signature { | ||
private static final String HMAC_ALG = "HmacSHA256" | ||
private static final String CHARSET = "UTF-8" | ||
public static final String MD_ALG = "SHA-256" | ||
|
||
String secret, auth, scheme, host, path, timestamp | ||
HashMap requestHeaders, postBody | ||
Method method | ||
|
||
String getSignature() { | ||
String signingKey = sign(timestamp, secret.getBytes(CHARSET)) | ||
String toSign = "${canonicalRequest}${auth}" | ||
sign(toSign, signingKey.getBytes(CHARSET)) | ||
} | ||
|
||
private String getCanonicalRequest(){ | ||
"${method.toString()}\t${scheme}\t${host}\t${path}\t${canonicalizeHeaders}\t${contentHash}\t" | ||
} | ||
|
||
private String getCanonicalizeHeaders(){ | ||
requestHeaders?requestHeaders.inject(''){ str, key, value -> | ||
value = (value.trim() =~ /s+/).replaceAll(' ') | ||
if(value){ | ||
str += "${key.toLowerCase()}:${value}\t" | ||
} | ||
} : EMPTY | ||
} | ||
|
||
private String getContentHash(){ | ||
String hash = EMPTY | ||
if(method == POST && postBody){ | ||
String body = JsonOutput.toJson(postBody) | ||
MessageDigest md = MessageDigest.getInstance(MD_ALG) | ||
byte[] bytes = body.bytes | ||
md.update(bytes, 0, bytes.length) | ||
byte[] digest = md.digest() | ||
hash = encodeBase64String(digest) | ||
} | ||
hash | ||
} | ||
|
||
private static String sign(String s, byte[] key) { | ||
SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_ALG) | ||
Mac mac = getInstance(HMAC_ALG) | ||
mac.init(signingKey) | ||
byte[] valueBytes = s.getBytes(CHARSET) | ||
byte[] bytes = mac.doFinal(valueBytes) | ||
encodeBase64String(bytes) | ||
} | ||
} |
Oops, something went wrong.