Monday, September 15, 2014

How to get username from http basic authentication in Oracle OSB

How to get username from http basic authentication in OSB
OSB needs to start with following java option
-Dcom.bea.wli.sb.transports.http.GetHttpAuthorizationHeaderAllowed=true
Then use the following expression to get the authorization header  in osb
$inbound/ctx:transport/ctx:request/tp:headers/tp:user-header[@name='Authorization']/@value
Above value is in the base 64 encoded format. To  decode :
Create a class as following, and use Java callout activity
package com.osb.custom.functions;
import javax.xml.bind.DatatypeConverter;
public class Base64Utitlity {
    public Base64Utitlity() {
        super();
    }
    public static String decode (String encodedStr) {
        byte[] decodedStr = DatatypeConverter.parseBase64Binary(encodedStr);
        return new String(decodedStr);
    }

    public static void main(String[] args) {
        Base64Utitlity base64Utitlity = new Base64Utitlity();
    }
}

To extract out the username , use following expression:
Input to java callout :
fn:substring-after($inbound/ctx:transport/ctx:request/tp:headers/tp:user-header[@name='Authorization']/@value, ' ')
Assign the java callout to a variable , say $temp
Then use following expression
fn:substring-before($temp, ':')