XQuery/Basic Authentication

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

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 | edit source]

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)

Returns the following:

   <password>open sesame</password>

Sample HTTP GET using Basic Authentication[edit | edit source]

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  := 
      <header name="Authorization" value="Basic {$credentials}"/>
  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 | edit source]