Thursday, 14 May 2009

ruby ResolverScriptAttributeDefinition in shibboleth2.1

As advertised at shibboleth2.1 can leverage scripting languages as jruby via jsr223.

Unfortunately documentation just covers ecmascript, no examples for ruby.

Here follows a working example:


<resolver:AttributeDefinition id="example3" xsi:type="Script"
language="ruby" xmlns="urn:mace:shibboleth:2.0:resolver:ad">
<resolver:Dependency ref="ldap" />
<resolver:Dependency ref="example2" />
<resolver:Dependency ref="example1" />
<resolver:AttributeEncoder xsi:type="SAML1String" xmlns="urn:mace:shibboleth:2.0:attribute:encoder"
name="urn:mace:unimore.it:attribute-def:example3" />
<resolver:AttributeEncoder xsi:type="SAML2String" xmlns="urn:mace:shibboleth:2.0:attribute:encoder"
name="urn:mace:unimore.it:attribute-def:example3" friendlyName="example3" />
<Script>
<![CDATA[
include Java

include_class 'edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute'

$example3 = BasicAttribute.new("example3")
[$example1, $example2].each do |array|
array.get_values.each do |v|
$example1.get_values.add v
end
end
]]>
</Script>
</resolver:AttributeDefinition>


In this example values from example1 and example2 fields are merged in the new example3 attribute.

Key point to note are:
* variables are exported from shibboleth as globals ($ prefix);
* as import_package did not work for me, just including edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute is enought;
* values of attribute are of type Java::JavaUtil::ArrayList. I'm not really sure normal ruby array operation work (maybe yes);
* puts can be useful with aacli.sh: it prints on the console.