View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2013, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(http_ax,
   36          [ http_ax_attributes/2,       % +Spec, -AttributeList
   37            ax_form_attributes/2        % +Form, -Values
   38          ]).   39:- use_module(library(error)).

Attribute Exchange library

This library can be used to create HTTP request parameters and analyse form-data for attribute exchange. Attribute exchange (AX) is used by OpenID and OAuth to fetch attributes for accounts, such as the real username or e-mail address. */

 http_ax_attributes(+Spec, -HTTPAttributes) is det
True when HTTPAttributes is a list of Name=Value pairs that can be used with an HTTP request to query for the attributes Spec. Spec is a list of elements Alias(Value[, Options]). Options include:
required
The attribute is required. This is mutually exclusive with if_available.
if_available
Only provide the attribute if it is available. This is mutually exclusive with required. This is the default.
url(+URL)
Can be used to ovcerrule or extend the ax_alias/2.
count(+Count)
Maximum number of values to provide

For example:

?- http_ax_attributes([ nickname(Nick),
                        email(Email, [required])
                      ], Params).
Params = [ 'openid.ax.mode'          = fetch_request,
           'openid.ax.type.nickname' = 'http://axschema.org/namePerson/friendly',
           'openid.ax.type.email'    = 'http://axschema.org/contact/email',
           'openid.ax.required'      = email,
           'openid.ax.if_available'  = nickname
         ].
   82http_ax_attributes(Spec, [ 'openid.ns.ax'   = 'http://openid.net/srv/ax/1.0',
   83                           'openid.ax.mode' = fetch_request
   84                         | AllAttr
   85                         ]) :-
   86    maplist(type_alias, Spec, AliasAttrs),
   87    partition(required, Spec, Required, Optional),
   88    alias_list(Required, 'openid.ax.required', RequiredAttr),
   89    alias_list(Optional, 'openid.ax.if_available', IfAvailableAttr),
   90    count_attr(Spec, CountAttr),
   91    append([AliasAttrs, RequiredAttr, IfAvailableAttr, CountAttr], AllAttr).
   92
   93type_alias(Spec, Attr=URL) :-
   94    functor(Spec, Alias, Arity),
   95    (   Arity > 1,
   96        arg(2, Spec, Options),
   97        memberchk(url(URL), Options)
   98    ->  true
   99    ;   ax_alias(Alias, URL)
  100    ->  true
  101    ;   existence_error(ax_alias, Alias)
  102    ),
  103    atom_concat('openid.ax.type.', Alias, Attr).
  104
  105required(Spec) :-
  106    functor(Spec, _, 2),
  107    arg(2, Spec, Options),
  108    memberchk(required, Options).
  109
  110alias_list([], _, []).
  111alias_list(Specs, A, [A=V]) :-
  112    maplist(alias_name, Specs, Aliases),
  113    atomic_list_concat(Aliases, ',', V).
  114
  115alias_name(Spec, Alias) :-
  116    functor(Spec, Alias, _).
  117
  118count_attr([], []).
  119count_attr([Spec|T0], [A=Count|T]) :-
  120    functor(Spec, Alias, 2),
  121    arg(2, Spec, Options),
  122    memberchk(count(Count), Options),
  123    !,
  124    atomic_list_concat('openid.ax.count.', Alias, A),
  125    count_attr(T0, T).
  126count_attr([_|T0], T) :-
  127    count_attr(T0, T).
 ax_alias(?Alias, ?URL) is nondet
True when Alias is an alias name for the AX schema URL. This predicate is defined as multifile.

Note that Google federated login only supports email, country, language, firstname and lastname.

  138:- multifile
  139    ax_alias/2.  140
  141ax_alias(nickname,  'http://axschema.org/namePerson/friendly').
  142ax_alias(email,     'http://axschema.org/contact/email').
  143ax_alias(fullname,  'http://axschema.org/namePerson').
  144ax_alias(dob,       'http://axschema.org/birthDate').
  145ax_alias(gender,    'http://axschema.org/person/gender').
  146ax_alias(postcode,  'http://axschema.org/contact/postalCode/home').
  147ax_alias(country,   'http://axschema.org/contact/country/home').
  148ax_alias(language,  'http://axschema.org/pref/language').
  149ax_alias(timezone,  'http://axschema.org/pref/timezone').
  150ax_alias(prefix,    'http://axschema.org/namePerson/prefix').
  151ax_alias(firstname, 'http://axschema.org/namePerson/first').
  152ax_alias(lastname,  'http://axschema.org/namePerson/last').
  153ax_alias(suffix,    'http://axschema.org/namePerson/suffix').
  154
  155
  156                 /*******************************
  157                 *            RESPONSE          *
  158                 *******************************/
 ax_form_attributes(+Form, -Values) is det
True if Values is a list Alias(Value) for each exchanged attribute.

Note that we assume we get the same alias names as we used for requesting the data. Not sure whether this is true.

Arguments:
Form- is an HTTP form as returned using the form(Form) option of http_parameters/3.
  171ax_form_attributes(Form, Values) :-
  172    (   memberchk('openid.ax.mode'=fetch_response, Form)
  173    ->  Ext = ax
  174    ;   memberchk(ExtNS='http://openid.net/srv/ax/1.0', Form),
  175        atomic_list_concat([openid,ns,Ext], '.', ExtNS)
  176    ->  true
  177    ),
  178    ax_attributes(Form, Ext, Values).
  179ax_form_attributes(_, []).
  180
  181ax_attributes([], _, []).
  182ax_attributes([Name=Value|T0], Ext, AXs) :-
  183    atomic_list_concat([openid, Ext, value, Alias|_Num], '.', Name),
  184    !,
  185    AX =.. [Alias,Value],
  186    AXs = [AX|AXT],
  187    ax_attributes(T0, Ext, AXT).
  188ax_attributes([_|T0], Ext, AXs) :-
  189    ax_attributes(T0, Ext, AXs)