1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2017, CWI 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(auth_config_unity, []).   36:- use_module(swish(lib/oauth2)).   37:- use_module(swish(lib/plugin/login)).   38
   39:- use_module(library(http/http_dispatch)).   40:- use_module(library(http/http_session)).   41:- use_module(library(http/http_json)).   42:- use_module(library(http/http_path)).   43:- use_module(library(debug)).

Enable login with a Unity-idm server

Client config to login to a Unity-idm server using oauth2. This oauth2 config has the following properties:

See also
- http://www.unity-idm.eu/ */
   63:- multifile
   64    oauth2:login/3,
   65    oauth2:server_attribute/3,
   66    swish_config:login_item/2,          % -Server, -HTML_DOM
   67    swish_config:login/2,               % +Server, +Request
   68    swish_config:user_info/2.           % +Request, ?Server, -Info
   69
   70:- http_set_session_options([create(noauto)]).   71
   72:- http_handler(swish(logout), unity_logout, []).   73
   74swish_config:login_item(unity, 10-Item) :-
   75    http_absolute_location(icons('unity.png'), Img, []),
   76    Item = img([ src(Img),
   77                 class('login-with'),
   78                 'data-server'(unity),
   79                 'data-frame'(popup),
   80                 title('Login with Unity')
   81               ]).
   82
   83swish_config:login(unity, Request) :-
   84    oauth2_login(Request, [server(unity)]).
   85
   86oauth2:login(_Request, unity, TokenInfo) :-
   87    debug(oauth, 'Token: ~p', [TokenInfo]),
   88    oauth2_user_info(unity, TokenInfo, Claim),
   89    debug(oauth, 'Claim: ~p', [Claim]),
   90    map_user_info(Claim, UserInfo),
   91    http_open_session(_SessionID, []),
   92    session_remove_user_data,
   93    http_session_assert(oauth2(unity, TokenInfo)),
   94    http_session_assert(user_info(unity, UserInfo)),
   95    reply_logged_in([ identity_provider('Unity'),
   96                      name(UserInfo.name),
   97                      user_info(UserInfo)
   98                    ]).
 map_user_info(+OAuthInfo, -UserInfo) is det
u{user:User, group:Group, name:Name, email:Email}
  104map_user_info(Dict0, Dict) :-
  105    dict_pairs(Dict0, Tag, Pairs0),
  106    maplist(map_user_field, Pairs0, Pairs),
  107    http_link_to_id(unity_logout, [], LogoutURL),
  108    dict_pairs(Dict, Tag,
  109               [ auth_method-oauth2,
  110                 logout_url-LogoutURL,
  111                 identity_provider-unity
  112               | Pairs
  113               ]).
  114
  115map_user_field(cn-Name, name-Name) :- !.
  116map_user_field(Field, Field).
 unity_logout(+Request)
Logout by removing the session data
  122unity_logout(_Request) :-
  123    catch(session_remove_user_data, _, true),
  124    reply_logged_out([]).
  125
  126session_remove_user_data :-
  127    http_session_retractall(oauth2(_,_)),
  128    http_session_retractall(user_info(_,_)).
 swish_config:user_info(+Request, ?Server, -Info:dict) is semidet
True if Info represents describes the currently logged in user.
  134swish_config:user_info(_Request, unity, UserInfo) :-
  135    http_in_session(_SessionID),
  136    http_session_data(user_info(unity, UserInfo)).
 oauth2:server_attribute(?Server, ?Attribute, ?Value)
EDIT:
  147oauth2:server_attribute(unity, url,            'https://woezel.ia.cwi.nl:2443').
  148oauth2:server_attribute(unity, redirect_uri,
  149                        'http://logicmoo.org/oauth2/unity/reply').
  150oauth2:server_attribute(unity, authorization_endpoint,
  151                        '/oauth2-as/oauth2-authz').
  152oauth2:server_attribute(unity, token_endpoint,     '/oauth2/token').
  153oauth2:server_attribute(unity, userinfo_endpoint,  '/oauth2/userinfo').
  154oauth2:server_attribute(unity, tokeninfo_endpoint, '/oauth2/tokeninfo').
  155oauth2:server_attribute(unity, client_id,          '****').
  156oauth2:server_attribute(unity, client_secret,      '****').
  157oauth2:server_attribute(unity, scope,              profile).
  158% EDIT: We use a self-signed certificate.  Remove/comment for production
  159oauth2:server_attribute(unity, cert_verify_hook,   cert_accept_any)