View source with raw comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2014-2016, 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(swish_config,
   36	  [ swish_reply_config/2,	% +Request, +Options
   37	    swish_config/2,		% ?Type, ?Config
   38	    swish_config_hash/2		% -HASH, +Options
   39	  ]).   40:- use_module(library(http/http_dispatch)).   41:- use_module(library(http/http_json)).   42:- use_module(library(option)).   43
   44:- multifile
   45	config/2,			% ?Key, ?Value
   46	config/3,			% ?Key, ?Value, +Options
   47	source_alias/2,			% ?Alias, ?Options
   48	authenticate/2,			% +Request, -User
   49        login_item/2,                   % -Server, -HTML_DOM
   50        login/2,                        % +Server, +Request
   51        user_info/3.                    % +Request, -Server, -Info

Make HTTP locations known to JSON code

*/

   56		 /*******************************
   57		 *	       CONFIG		*
   58		 *******************************/
 swish_reply_config(+Request, +Options) is semidet
Emit a configuration object to the client if the client requests for '.../swish_config.json', regardless of the path prefix.
   65swish_reply_config(Request, Options) :-
   66	option(path(Path), Request),
   67	file_base_name(Path, 'swish_config.json'),
   68	json_config(JSON, Options),
   69	reply_json(JSON).
 swish_config_hash(-Hash, +Options) is det
True if Hash is the SHA1 of the SWISH config.
   75swish_config_hash(Hash, Options) :-
   76	json_config(Config, Options),
   77	variant_sha1(Config, Hash).
   78
   79json_config(json{ http: json{ locations:JSON
   80			    },
   81		  swish: SWISHConfig
   82		}, Options) :-
   83	http_locations(JSON),
   84	swish_config_dict(SWISHConfig, Options).
   85
   86http_locations(JSON) :-
   87	findall(ID-Path,
   88		( http_current_handler(Path, _:_, Options),
   89		  memberchk(id(ID), Options)
   90		), Pairs),
   91	keysort(Pairs, Sorted),
   92	remove_duplicate_ids(Sorted, Cleaned),
   93	dict_pairs(JSON, json, Cleaned).
   94
   95remove_duplicate_ids([], []).
   96remove_duplicate_ids([Id-Path1,Id-Path2|T], [Id-Path1|Cleaned]) :- !,
   97	same_ids(T, Id, T1, Paths0),
   98	sort([Path1,Path2|Paths0], Unique),
   99	(   Unique = [_]
  100	->  true
  101	;   print_message(warning, http(duplicate_handlers(Id, Unique)))
  102	),
  103	remove_duplicate_ids(T1, Cleaned).
  104remove_duplicate_ids([H|T0], [H|T]) :-
  105	remove_duplicate_ids(T0, T).
  106
  107same_ids([], _, [], []).
  108same_ids([Id-Path|T0], Id, T, [Path|TP]) :- !,
  109	same_ids(T0, Id, T, TP).
  110same_ids(T, _, T, []).
 swish_config_dict(-Config:dict, +Options) is det
Obtain name-value pairs from swish_config:config/2
  117swish_config_dict(Config, Options) :-
  118	findall(Key-Value, swish_config(Key, Value, Options), Pairs),
  119	dict_pairs(Config, json, Pairs).
 config(-Key, -Value) is nondet
 swish_config(-Key, -Value) is nondet
Define a name/value pair that will end up in the SWISH config object (see web/js/config.js)
  127swish_config(Key, Value) :-
  128	swish_config(Key, Value, []).
  129
  130swish_config(Key, Value, Options) :-
  131	config(Key, Value, Options).
  132swish_config(Key, Value, _) :-
  133	config(Key, Value).
  134
  135% We need to use '$swish wrapper' with a variable _residuals in
  136% versions that support the `var_prefix` option.
  137:- if(current_prolog_flag(var_prefix, _)).  138config(residuals_var, '_residuals').
  139:- endif.  140
  141		 /*******************************
  142		 *             LOGIN		*
  143		 *******************************/
 login_item(-Server, -Item) is nondet
This hook is called to find all possible login options. It should bind Item to an HTML description for html//1 that must be clicked to login with this option. The item may have the following HTML attributes:
data-server(+Server)
This must be present and provides the first argument for the login/2 hook.
data-frame(+Style)
The login is realised in a popup to avoid reloading the current swish page. If Style is popup, a browser popup window is used. This is necessary for identity providers that refuse to open inside a frame. The default is iframe, which handles the login inside an iframe element in a modal popup.

The Item is often an image. The image must have a class login-with. Below is an example to login with Google:

swish_config:login_item(Item) :-
    http_absolute_location(icons('social_google_box.png'), Img, []),
    Item = img([ src(Img),
                 class('login-with'),
                 'data-server'(google),
                 title('Login with Google')
               ]).
Arguments:
Item- may be of the form Tag-Item. In this case the items are ordered by Tag. The default tag is 0.
 login(+Server, +Request) is det
If a login item with 'data-server'(+Server) is clicked, the HTTP handler with id login is called. This handler figures the selected login server and calls this hook.
 user_info(+Request, -Server, -UserInfo:dict) is semidet
Each login facility must provide this hook. The hook should succeed if-and-only-if the user is logged in using this facility and the hook must bind UserInfo with a dict that contains the following fields:
user:User
User name (id) if the logged in user.
name:Name
Common name of the logged in user.
email:Email
Email address of the logged in user.
picture:URL
If present, URL is used to indicate the currently logged in user.
auth_method:Method
Authentication method used. Currently one of basic, digest or oauth2.
logout_url:URL
URL that must be used to logout. Needed if auth_method is not one of the HTTP authentication methods (basic or digest).

If this hook fails the user is not logged in.

  212		 /*******************************
  213		 *          OTHER HOOKS		*
  214		 *******************************/
 source_alias(?Alias, ?Options) is nondet
Multifile hook that defines properties of file_search_path/2 aliases wrt. storage handling. Defined options are:
access(Access)
One of read or both.
search(Pattern)
The New Tab search form searches in files that satisfy the given pattern in the matching directories. Pattern is handed to expand_file_name/2.
  229		 /*******************************
  230		 *	      MESSAGES		*
  231		 *******************************/
  232
  233:- multifile
  234	prolog:message//1.  235
  236prolog:message(http(duplicate_handlers(Id, Paths))) -->
  237	[ 'Duplicate HTTP handler IDs: "~w"'-[Id] ],
  238	paths(Paths).
  239
  240paths([]) --> [].
  241paths([H|T]) --> [ '\t~q'-[H], nl ], paths(T)