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
56 /******************************* 57 * CONFIG * 58 *******************************/
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).
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, []).
117swish_config_dict(Config, Options) :-
118 findall(Key-Value, swish_config(Key, Value, Options), Pairs),
119 dict_pairs(Config, json, Pairs).
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 *******************************/
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') ]).
'data-server'(+Server)
is clicked, the
HTTP handler with id login
is called. This handler figures the
selected login server and calls this hook.basic
, digest
or oauth2
.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 *******************************/
read
or both
.229 /******************************* 230 * MESSAGES * 231 *******************************/ 232 233:- multifile 234 prolog:message//1. 235 236prologmessage(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)
Make HTTP locations known to JSON code
*/