Skip to content

Instantly share code, notes, and snippets.

@joemccall86
Created November 14, 2013 00:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemccall86/7459214 to your computer and use it in GitHub Desktop.
Save joemccall86/7459214 to your computer and use it in GitHub Desktop.
Google Maps API for Business HTTPBuilder. Based on https://developers.google.com/maps/documentation/business/webservices/auth#digital_signatures Due to the way signing works, it's insufficient to simply add a signature query parameter to the `uri.query` map since it must be appended at the end, and it must be unmodified by url encoding. This int…
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.URIBuilder
import org.apache.http.client.ClientProtocolException
import org.springframework.beans.factory.InitializingBean
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
/**
* Created by Joe on 11/13/13.
*/
class GMESignedHTTPBuilder extends HTTPBuilder implements InitializingBean {
def signer
String clientId
String secret
// write-only for security reasons
private getSecret() { '****' }
void afterPropertiesSet() {
assert clientId != null
assert secret != null
def key = secret
.replace('-', '+')
.replace('_', '/')
.decodeBase64()
def algorithm = 'HmacSHA1'
signer = Mac.getInstance algorithm
signer.init new SecretKeySpec(key, algorithm)
}
@Override
protected Object doRequest(final HTTPBuilder.RequestConfigDelegate delegate)
throws ClientProtocolException, IOException {
delegate.uri.addQueryParam 'client', this.clientId
def signature = signUri(delegate.uri as URI)
delegate.uri = new URIBuilder("${delegate.uri}&signature=${signature}")
super.doRequest(delegate)
}
private def signUri(final URI uri) {
def resource = uri.with { "${rawPath}?${rawQuery}" }
String signature = signer.doFinal(resource.bytes)
.encodeBase64()
signature = signature
.replace('+', '-')
.replace('/', '_')
signature
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment