3

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
cjstehno
  • 13,468
  • 4
  • 44
  • 56
Vinay
  • 33
  • 4
  • You should include what server you are using, as well as the server logs related to this. – cjstehno Jul 12 '17 at 23:18
  • I tested against a simple spring-boot application (tomcat) with spring-security (basic and digest) and authentication works as expected. It has also been tested against Undertow. I would say that their is something either different or wrong with your server - please feel free to create an issue on the project if you find that your server is for some reason not supported properly. – cjstehno Jul 13 '17 at 16:06
  • Thanks for your reply, we are using mulesoft 3.8.0 enterprise edition server for REST endpoint. When I try with postman with wrong credencials I can see error in mule logs, but I don't see any logs when I try through script. – Vinay Jul 14 '17 at 05:54

2 Answers2

2

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
mnd
  • 2,709
  • 3
  • 27
  • 48
1

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/

  • Tried your suggestion Aaron, i am still getting "groovyx.net.http.HttpException: Unauthorized" – Vinay Jul 17 '17 at 04:05
  • You're right! I did a quick ratpack server example and while using curl, I can clearly see the authentication come over, I didn't see it using http builder ng, seems like a bug you should file with the project. – Aaron Hagopian Jul 17 '17 at 16:52
  • If either of you can provide a standalone example in the bug request, we can take a look. Thanks. – cjstehno Jul 18 '17 at 15:03