1
- # Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
2
- #
3
- # This program is free software; you can redistribute it and/or
4
- # modify it under the terms of the GNU General Public License
5
- # as published by the Free Software Foundation; either version 2
6
- # of the License, or (at your option) any later version.
7
- #
8
- # This program is distributed in the hope that it will be useful,
9
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
- # GNU General Public License for more details, published at
12
- # http://www.gnu.org/copyleft/gpl.html
1
+ # See bottom of file for notices
13
2
14
3
=pod
15
4
16
5
---+ package Foswiki::Plugins::HttpsRedirectPlugin
17
6
18
- To interact with Foswiki use ONLY the official API functions
19
- in the Foswiki::Func module. Do not reference any functions or
20
- variables elsewhere in Foswiki, as these are subject to change
21
- without prior warning, and your plugin may suddenly stop
22
- working.
23
-
24
- For increased performance, all handlers except initPlugin are
25
- disabled below. *To enable a handler* remove the leading DISABLE_ from
26
- the function name. For efficiency and clarity, you should comment out or
27
- delete the whole of handlers you don't use before you release your
28
- plugin.
29
-
30
- __NOTE:__ When developing a plugin it is important to remember that
31
- Foswiki is tolerant of plugins that do not compile. In this case,
32
- the failure will be silent but the plugin will not be available.
33
- See [[%SYSTEMWEB%.Plugins#FAILEDPLUGINS]] for error messages.
34
-
35
- __NOTE:__ Defining deprecated handlers will cause the handlers to be
36
- listed in [[%SYSTEMWEB%.Plugins#FAILEDPLUGINS]]. See
37
- [[%SYSTEMWEB%.Plugins#Handlig_deprecated_functions]]
38
- for information on regarding deprecated handlers that are defined for
39
- compatibility with older Foswiki versions.
40
-
41
- __NOTE:__ When writing handlers, keep in mind that these may be invoked
42
- on included topics. For example, if a plugin generates links to the current
43
- topic, these need to be generated before the afterCommonTagsHandler is run,
44
- as at that point in the rendering loop we have lost the information that we
45
- the text had been included from another topic.
7
+ Intercept any http: requests that should be done over https
8
+ and redirect the requests to an https: URL
9
+ * Any request to login
10
+ * Requests to any script in the ={AuthScripts}= list
11
+ * Any request that triggers a forceAuthentication event.
46
12
47
13
=cut
48
14
49
15
package Foswiki::Plugins::HttpsRedirectPlugin ;
50
16
51
- # Always use strict to enforce variable scoping
52
17
use strict;
18
+ use warnings;
53
19
54
- require Foswiki::Func; # The plugins API
55
- require Foswiki::Plugins; # For the API version
20
+ use Foswiki::Func; # The plugins API
21
+ use Foswiki::Plugins; # For the API version
56
22
57
- our $VERSION = ' 1.2 ' ;
58
- our $RELEASE = ' 19 Mar 2016 ' ;
23
+ our $VERSION = ' 1.3 ' ;
24
+ our $RELEASE = ' 26 Mar 2017 ' ;
59
25
60
- # Short description of this plugin
61
- # One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
62
- our $SHORTDESCRIPTION = ' Redirect authenticated users to HTTPS url.' ;
63
-
64
- # You must set $NO_PREFS_IN_TOPIC to 0 if you want your plugin to use preferences
65
- # stored in the plugin topic.
26
+ our $SHORTDESCRIPTION = ' Redirect authenticated users to HTTPS url.' ;
66
27
our $NO_PREFS_IN_TOPIC = 1;
67
28
68
29
# Name of this Plugin, only used in this module
@@ -72,12 +33,37 @@ our $debug = 0;
72
33
73
34
=pod
74
35
36
+ ---++ earlyInitPlugin
37
+
38
+ Determines if TemplateLogin is in use. If it is, enable a hook that
39
+ monkey-patches TemplateLogin::forceAuthentication. This hook will force
40
+ a redirect to https if any page is accessed that would require authentication.
41
+
42
+ =cut
43
+
44
+ sub earlyInitPlugin {
45
+ return if !$Foswiki::cfg {Plugins }{HttpsRedirectPlugin }{Enabled };
46
+ return undef
47
+ unless (
48
+ $Foswiki::cfg {LoginManager } eq ' Foswiki::LoginManager::TemplateLogin' );
49
+ require Foswiki::Plugins::HttpsRedirectPlugin::CoreHooks;
50
+ Foswiki::Plugins::HttpsRedirectPlugin::CoreHooks::hook();
51
+ return undef ;
52
+ }
53
+
54
+ =pod
55
+
75
56
---++ initPlugin($topic, $web, $user, $installWeb) -> $boolean
76
57
* =$topic= - the name of the topic in the current CGI query
77
58
* =$web= - the name of the web in the current CGI query
78
59
* =$user= - the login name of the user
79
60
* =$installWeb= - the name of the web the plugin is installed in
80
61
62
+ Redirects requests to https:
63
+ * Any request to login
64
+ * Requests to any ={AuthScripts}=
65
+ * Any authenticated requests.
66
+
81
67
=cut
82
68
83
69
sub initPlugin {
@@ -90,74 +76,86 @@ sub initPlugin {
90
76
return 0;
91
77
}
92
78
93
- # Example code of how to get a preference value, register a variable handler
94
- # and register a RESTHandler. (remove code you do not need)
95
-
96
- # Set plugin preferences in LocalSite.cfg, like this:
97
- # $Foswiki::cfg{Plugins}{HttpsRedirectPlugin}{ExampleSetting} = 1;
98
- # Always provide a default in case the setting is not defined in
99
- # LocalSite.cfg. See %SYSTEMWEB%.Plugins for help in adding your plugin
100
- # configuration to the =configure= interface.
101
79
$debug = $Foswiki::cfg {Plugins }{HttpsRedirectPlugin }{Debug } || 0;
102
80
81
+ my $query = Foswiki::Func::getCgiQuery();
82
+
83
+ return 1 if $query -> secure(); # Nothing needed if already secure
84
+ return 1
85
+ if Foswiki::Func::getContext()-> {' command_line' }; # Not needed for CLI
86
+
103
87
if (Foswiki::Func::isGuest) {
104
88
105
- # If we are guest, force HTTPS on login
106
- if ( Foswiki::Func::getContext()
107
- -> {' login' } ) # If we are on the login script
108
- {
89
+ my $actionRegex =
90
+ ' \b' . Foswiki::Func::getRequestObject()-> action() . ' \b' ;
109
91
110
- # Build up our URL
111
- my $query = Foswiki::Func::getCgiQuery();
112
- my $url = $query -> url() . $query -> path_info();
113
- if ( $query -> query_string() ) {
114
- $url .= ' ?' . $query -> query_string();
115
- }
116
-
117
- unless ( $url =~ / ^https/ ) # Unless we are already using HTTPS
118
- {
119
-
120
- # Redirect to HTTPS URL and quite
121
- $url =~ s / ^http/ https/ ;
122
- Foswiki::Func::writeDebug(" HTTPS redirect to: $url " )
123
- if ($debug );
124
- Foswiki::Func::redirectCgiQuery( $query , $url );
125
-
126
- # $Foswiki::Plugins::SESSION->finish();
127
- # exit(0);
128
- }
92
+ # If we are guest, force HTTPS on login, or any script that requires authentication.
93
+ if (
94
+ Foswiki::Func::getContext()
95
+ -> {' login' } # If we are on the login script
96
+ || $Foswiki::cfg {AuthScripts } =~ m /$actionRegex /
97
+ )
98
+ {
99
+ _redirectRequest($query );
129
100
}
130
-
131
101
}
132
102
else {
103
+ # Force redirect on all requests
104
+ _redirectRequest($query );
133
105
134
- # If the user is no guest always force HTTPS
106
+ }
135
107
136
- # Get our URL
137
- my $query = Foswiki::Func::getCgiQuery();
138
- my $url = $query -> url() . $query -> path_info();
139
- if ( $query -> query_string() ) {
140
- $url .= ' ?' . $query -> query_string();
141
- }
108
+ # Plugin correctly initialized
109
+ return 1;
110
+ }
142
111
143
- # Unless we are already using HTTPS, or running from CLI
144
- unless ( $url =~ / ^https/
145
- or Foswiki::Func::getContext()-> {' command_line' } )
146
- {
112
+ =pod
113
+ ---++ Private _redirectRequest($query)
147
114
148
- # Redirect to HTTPS URL and quite
149
- $url =~ s / ^http/ https/ ;
150
- Foswiki::Func::writeDebug(" HTTPS redirect to: $url " ) if ($debug );
151
- Foswiki::Func::redirectCgiQuery( $query , $url );
115
+ Convert the URL to https: and redirect.
152
116
153
- # $Foswiki::Plugins::SESSION->finish();
154
- # exit(0);
155
- }
156
- }
117
+ =cut
157
118
119
+ sub _redirectRequest {
120
+ my $query = shift ;
158
121
159
- # Plugin correctly initialized
160
- return 1;
122
+ # Get our URL
123
+ my $url = $query -> url() . $query -> path_info();
124
+ if ( $query -> query_string() ) {
125
+ $url .= ' ?' . $query -> query_string();
126
+ }
127
+
128
+ # Redirect to HTTPS URL
129
+ $url =~ s / ^http/ https/ ;
130
+ $url = Foswiki::decode_utf8($url ) if $Foswiki::UNICODE ;
131
+ Foswiki::Func::writeDebug(" HTTPS redirect to: $url " ) if ($debug );
132
+ Foswiki::Func::redirectCgiQuery( $query , $url );
161
133
}
162
134
163
135
1;
136
+ __END__
137
+
138
+ Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
139
+
140
+ Copyright (C) 2008-2017 Foswiki Contributors. Foswiki Contributors
141
+ are listed in the AUTHORS file in the root of this distribution.
142
+ NOTE: Please extend that file, not this notice.
143
+
144
+ Additional copyrights may apply to some or all of the code in this
145
+ file as follows:
146
+
147
+ Copyright (C) 2013 Modell Aachen GmbH, http://modell-aachen.de
148
+ Author: Jan Krueger
149
+
150
+ Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
151
+ This program is free software; you can redistribute it and/or
152
+ modify it under the terms of the GNU General Public License
153
+ as published by the Free Software Foundation; either version 2
154
+ of the License, or (at your option) any later version.
155
+
156
+ This program is distributed in the hope that it will be useful,
157
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
158
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
159
+ GNU General Public License for more details, published at
160
+ http://www.gnu.org/copyleft/gpl.html
161
+
0 commit comments