XQuery/Basic Authentication

Motivation edit

You want to use a very basic login process over a secure network such as a secure Intranet or over an SSL connection.

Method edit

We will used the base64 encoding and decoding tools to generate the right strings.


xquery version "1.0";

let $user := 'Aladdin'
let $password := 'open sesame'
let $credentials := concat($user, ':', $password)
let $encode := util:string-to-binary($credentials)
return
<results>
   <user>{$user}</user>
   <password>{$password}</password>
   <encode>{$encode}</encode>
</results>

Returns the following:

<results>
   <user>Aladdin</user>
   <password>open sesame</password>
   <encode>QWxhZGRpbjpvcGVuIHNlc2FtZQ==</encode>
</results>

Sample HTTP GET using Basic Authentication edit

xquery version "1.0";

declare function local:basic-get-http($uri,$username,$password) {
  let $credentials := concat($username,":",$password)
  let $credentials := util:string-to-binary($credentials)
  let $headers  := 
    <headers>
      <header name="Authorization" value="Basic {$credentials}"/>
    </headers>
  return httpclient:get(xs:anyURI($uri),false(), $headers)
};

let $host := "http://localhost:8080"
let $path :=  "/exist/rest/db/apps/terms/data/1.xml"
let $uri := concat($host, $path)
let $user := 'my-login'
let $password := 'my-password'
return local:basic-get-http($uri,$username,$password)

References edit