Skip to content

Commit

Permalink
Further cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony Jones committed Aug 24, 2011
1 parent 35f7dd9 commit 67b262c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
Expand Up @@ -16,9 +16,8 @@ package org.grails.plugins.oauth
* limitations under the License.
*/

import org.codehaus.groovy.grails.commons.ConfigurationHolder as C

class OauthController {

def oauthService

/**
Expand Down Expand Up @@ -72,7 +71,6 @@ class OauthController {
*/
def callback = {
log.debug "Callback received..."

log.debug "Got callback params: $params"

// List session parameters
Expand Down
Expand Up @@ -63,10 +63,9 @@ import org.apache.http.client.methods.HttpRequestBase
* @author Antony Jones (Desirable Objects)
*/
class OauthService implements InitializingBean {
// Transactional service

boolean transactional = false

// Service properties
def providers = [:]
def consumers = [:]
private HttpClient httpClient
Expand Down Expand Up @@ -209,6 +208,7 @@ class OauthService implements InitializingBean {

consumers[name] = ['key': token?.key, 'secret': token?.secret, 'providerName': key]
}

} else {
log?.error "Error initializaing OauthService: No consumers defined!"
}
Expand Down Expand Up @@ -238,8 +238,11 @@ class OauthService implements InitializingBean {
log.debug "Authorisation URL: ${authorisationURL}\n"
log.debug "Is OAuth 1.0a: ${isOAuth10a}"

[key: consumer?.getToken(), secret: consumer?.getTokenSecret(), authUrl: authorisationURL,
isOAuth10a: isOAuth10a]
return [key: consumer?.getToken(),
secret: consumer?.getTokenSecret(),
authUrl: authorisationURL,
isOAuth10a: isOAuth10a
]

} catch (Exception ex) {
final def errorMessage = "Unable to fetch request token (consumerName=$consumerName)"
Expand Down Expand Up @@ -307,7 +310,7 @@ class OauthService implements InitializingBean {
throw new OauthServiceException(errorMessage, ex)
}

[key: accessToken, secret: tokenSecret]
return [key: accessToken, secret: tokenSecret]
}

/**
Expand Down Expand Up @@ -361,19 +364,21 @@ class OauthService implements InitializingBean {
* @throws OauthServiceException
* If {@code args.consumer} does not represent an existing consumer.
*/
def accessResource(final def Map args) {
OauthResponse accessResource(final def Map args) {

final HttpResponse response = doRequest(args)
OauthResponse oauthResponse = new OauthResponse()

try {
log.debug "Reading response body"

OauthResponse oauthResponse = new OauthResponse()

oauthResponse.with {
body = EntityUtils.toString(response.getEntity())
body = EntityUtils.toString(response.entity)
status = response.statusCode
}

log.debug "Response body read successfully"
return oauthResponse

} catch (Exception ex) {

Expand All @@ -383,7 +388,6 @@ class OauthService implements InitializingBean {

}

return oauthResponse
}

/**
Expand Down Expand Up @@ -411,7 +415,7 @@ class OauthService implements InitializingBean {
* @throws OauthServiceException
* If {@code args.consumer} does not represent an existing consumer.
*/
def doRequest(final def url, final String consumer, final def token,
HttpResponse doRequest(final def url, final String consumer, final def token,
final def method = 'GET', final def params = [:], final def headers = [:],
final def body = null, final def accept = null, final def contentType = null) {

Expand Down Expand Up @@ -442,7 +446,7 @@ class OauthService implements InitializingBean {
* @throws OauthServiceException
* If {@code args.consumer} does not represent an existing consumer.
*/
def doRequest(final Map args) {
HttpResponse doRequest(final Map args) {

final String consumer = args.consumer
def url = args.url
Expand Down Expand Up @@ -515,7 +519,6 @@ class OauthService implements InitializingBean {
requestMethod.setHeader("Accept", "$accept")
}

// Sign, execute and return HttpResponse
execute(requestMethod, consumer, token)
}

Expand Down Expand Up @@ -558,18 +561,16 @@ class OauthService implements InitializingBean {
* @throws OauthServiceException
* If {@code consumerName} does not represent an existing consumer.
*/
def HttpResponse execute(final HttpUriRequest request, final String consumerName,
HttpResponse execute(final HttpUriRequest request, final String consumerName,
final def accessToken) {
// Validate token
assertAccessToken(accessToken)

validateAccessToken(accessToken)

log.debug("Executing ${request?.getMethod()} to ${request?.getURI()}")

try {
// Sign the request
sign(request, consumerName, accessToken)

// Execute it, return the HttpResponse
sign(request, consumerName, accessToken)
httpClient.execute(request)

} catch (Exception ex) {
Expand Down Expand Up @@ -599,9 +600,9 @@ class OauthService implements InitializingBean {
* @throws OauthServiceException
* If {@code consumerName} does not represent an existing consumer.
*/
def sign(final HttpUriRequest request, final String consumerName, final def accessToken) {
// Check access token is valid
assertAccessToken(accessToken)
void sign(final HttpUriRequest request, final String consumerName, final def accessToken) {

validateAccessToken(accessToken)

try {
final OAuthConsumer consumer = getConsumer(consumerName)
Expand Down Expand Up @@ -646,7 +647,7 @@ class OauthService implements InitializingBean {
* @throws OauthServiceException
* If {@code consumerName} does not represent an existing provider.
*/
private def getProvider(final String consumerName) {
private CommonsHttpOAuthProvider getProvider(final String consumerName) {

final String providerName = consumers[consumerName]?.providerName

Expand All @@ -668,7 +669,7 @@ class OauthService implements InitializingBean {
* @param the token to validate.
* @throws OauthServiceException when provided token is invalid.
*/
private def assertAccessToken(def token) {
private void validateAccessToken(def token) {
if (!token || !token?.key || !token?.secret) {
final def errorMessage = "Invalid access token! (token=$token, token.key=${token?.key}, " +
"token.secret=${token?.secret})"
Expand Down

0 comments on commit 67b262c

Please sign in to comment.