basic auth http-builder-ng

1.8k views Asked by At

Basic Auth not working with http-builder-ng - Authorization request is not passed by ng?

build.gradle

group 'org.vinay.rest'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'
apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    // https://mvnrepository.com/artifact/io.github.http-builder-ng/http-builder-ng-core
    testRuntime group: 'io.github.http-builder-ng', name: 'http-builder-ng-core', version: '0.17.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'
    testCompile group: 'ch.qos.logback', name: 'logback-classic', version:'1.0.13'
}

GetD2LUserBySourcedIdSpec.groovy

import spock.lang.Specification
import groovyx.net.http.HttpBuilder

/**
 * Created by e5017581 on 11/07/2017.
 */
class GetD2LUserBySourcedIdSpec  extends Specification {
   def "test get /users call with d2lSourced Id"() {
       given:

       def httpClient = HttpBuilder.configure {
           request.uri = 'http://esbvucmv5.vu.edu.au/D2L/2.0'
           //request.headers['Authorization'] = 'Basic ZDJscnM6MTRkMmxycw=='
           //request.headers['Content-Type'] = "application/json"
           request.auth.basic'd2lrs', '14d2lrs'

       }
       when:
       def user = httpClient.get {
           //request.auth.basic 'd2lrs', '14d2lrs'
           //request.headers['Authorization'] = 'Basic ZDJscnM6MTRkMmxycw=='
           request.uri.path = '/users/'
           request.uri.query = [sourcedId: 's4504565']
       }
       then:
       println user.dump()
       null != user
   }
}

http-logs:

com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 "GetD2LUserBySourcedIdSpec,test get /users call with d2lSourced Id"
15:41:36,714 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.groovy] at [file:/C:/Users/e5017581/IdeaProjects/http-builder-ng2/build/resources/test/logback.groovy]
15:41:36,935 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@6d07a63d - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
15:41:36,946 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@6d07a63d - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:41:36,947 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@6d07a63d - Naming appender as [CONSOLE]
15:41:37.100 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Request-Header: Accept-Encoding -> [gzip, deflate]
15:41:37.306 [main] DEBUG g.net.http.JavaHttpBuilder.content - Response-Body: null
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Date -> Tue, 11 Jul 2017 05:41:37 GMT
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Content-Type -> text/plain
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Content-Length -> 42
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Connection -> keep-alive
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Server -> Mule/SNAPSHOT
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: WWW-Authenticate -> Basic realm="realm"
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: http.status -> 401
15:41:37.307 [main] DEBUG g.net.http.JavaHttpBuilder.headers - Response-Header: Set-Cookie -> VUpersist=rd2480o00000000000000000000ffff0a4c0016o80; path=/; Httponly

groovyx.net.http.HttpException: Unauthorized
at groovyx.net.http.NativeHandlers.failure(NativeHandlers.java:69)
at groovy.lang.Closure.call(Closure.java:423)
at groovyx.net.http.fn.ClosureBiFunction.apply(ClosureBiFunction.java:45)
at groovyx.net.http.HttpBuilder$ResponseHandlerFunction.apply(HttpBuilder.java:1834)
at groovyx.net.http.JavaHttpBuilder$Action.lambda$execute$2(JavaHttpBuilder.java:157)
at groovyx.net.http.JavaHttpBuilder$ThreadLocalAuth.with(JavaHttpBuilder.java:320)
at groovyx.net.http.JavaHttpBuilder$Action.execute(JavaHttpBuilder.java:111)
at groovyx.net.http.JavaHttpBuilder.createAndExecute(JavaHttpBuilder.java:362)
at groovyx.net.http.JavaHttpBuilder.doGet(JavaHttpBuilder.java:369)
at groovyx.net.http.HttpObjectConfigImpl.nullInterceptor(HttpObjectConfigImpl.java:45)
at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:360)
at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:1311)
at GetD2LUserBySourcedIdSpec.test get /users call with d2lSourced Id(GetD2LUserBySourcedIdSpec.groovy:19)


Process finished with exit code -1
2

There are 2 answers

3
Aaron Hagopian On

I had the same issue myself when I first tried this library out. The URI should not have any path in it, just the protocol and host (and port)

Your request URI should look like this:

request.uri = 'http://esbvucmv5.vu.edu.au'

And the Path in the get call would look like this:

request.uri.path = /D2L/2.0/users/

1
mnd On

Thanks to the GitHub issue link provided by @Vinay as well as the support of @cjstehno (creator of the helpful http-builder-ng project), I was able to get this working when making calls to Mule.

Normally this should work in http-builder-ng

request.auth.basic(username, password)

But that doesn't work with Mule, and you receive cryptic error messages about authentication, as seen in the question above.

The following workaround resolves this issue when calling Mule.

// Manually generate Base 64 encoding of username and password
String encodedAuthString = "Basic " + ("$username:$password".bytes.encodeBase64().toString())

// Set in the `HttpBuilder.configure` closure
request.headers['Authorization'] = encodedAuthString