Skip to content

Commit a47e118

Browse files
authored
Merge pull request #2 from curityio/bugfix/fix-usage-with-proxy
Fix the authenticator to work behind a proxy
2 parents 1af242a + deaddd5 commit a47e118

File tree

5 files changed

+63
-26
lines changed

5 files changed

+63
-26
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ This project provides an opens source StackExchange Authenticator plug-in for th
1313
System Requirements
1414
~~~~~~~~~~~~~~~~~~~
1515

16-
* Curity Identity Server 2.4.0 and `its system requirements <https://developer.curity.io/docs/latest/system-admin-guide/system-requirements.html>`_
16+
* Curity Identity Server 7.0.2 and `its system requirements <https://developer.curity.io/docs/latest/system-admin-guide/system-requirements.html>`_
1717

1818
Requirements for Building from Source
1919
"""""""""""""""""""""""""""""""""""""
2020

2121
* Maven 3
22-
* Java JDK v. 8
22+
* Java SDK 17 or later
2323

2424
Compiling the Plug-in from Source
2525
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.curity.identityserver.plugin</groupId>
88
<artifactId>identityserver.plugins.authenticators.stackexchange</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.2.0</version>
1010

1111
<name>StackExchange Authenticator</name>
1212

@@ -21,8 +21,8 @@
2121
<artifactId>maven-compiler-plugin</artifactId>
2222
<version>3.1</version>
2323
<configuration>
24-
<source>1.8</source>
25-
<target>1.8</target>
24+
<source>17</source>
25+
<target>17</target>
2626
</configuration>
2727
</plugin>
2828
</plugins>
@@ -32,12 +32,12 @@
3232
<dependency>
3333
<groupId>se.curity.identityserver</groupId>
3434
<artifactId>identityserver.sdk</artifactId>
35-
<version>2.4.0</version>
35+
<version>7.1.0</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>org.slf4j</groupId>
3939
<artifactId>slf4j-api</artifactId>
40-
<version>1.7.22</version>
40+
<version>1.7.36</version>
4141
</dependency>
4242
</dependencies>
4343

@@ -71,4 +71,4 @@
7171
<url>https://nexus.curity.se/nexus/content/repositories/customer-snapshot-repo/</url>
7272
</snapshotRepository>
7373
</distributionManagement>
74-
</project>
74+
</project>

src/main/java/io/curity/identityserver/plugin/stackexchange/authentication/CallbackRequestHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Objects;
5151
import java.util.Optional;
5252

53+
import static io.curity.identityserver.plugin.stackexchange.authentication.RedirectUriUtil.createRedirectUri;
5354
import static se.curity.identityserver.sdk.http.HttpRequest.createFormUrlEncodedBodyProcessor;
5455

5556
public class CallbackRequestHandler
@@ -208,12 +209,14 @@ private Map<String, Collection<String>> createQueryParameters(String accessToken
208209

209210
private Map<String, Object> redeemCodeForTokens(CallbackGetRequestModel requestModel)
210211
{
212+
var redirectUri = createRedirectUri(_authenticatorInformationProvider, _exceptionFactory);
213+
211214
URI uri = URI.create("https://stackexchange.com/oauth/access_token/json");
212215
HttpResponse tokenResponse = _webServiceClientFactory.create(uri)
213216
.request()
214217
.contentType("application/x-www-form-urlencoded")
215218
.body(createFormUrlEncodedBodyProcessor(createPostData(_config.getClientId(),
216-
_config.getClientSecret(), requestModel.getCode(), requestModel.getRequestUrl())))
219+
_config.getClientSecret(), requestModel.getCode(), redirectUri)))
217220
.post()
218221
.response();
219222
int statusCode = tokenResponse.statusCode();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2022 Curity AB
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.curity.identityserver.plugin.stackexchange.authentication;
18+
19+
import se.curity.identityserver.sdk.errors.ErrorCode;
20+
import se.curity.identityserver.sdk.service.ExceptionFactory;
21+
import se.curity.identityserver.sdk.service.authentication.AuthenticatorInformationProvider;
22+
23+
import java.net.MalformedURLException;
24+
import java.net.URL;
25+
26+
import static io.curity.identityserver.plugin.stackexchange.descriptor.StackExchangeAuthenticatorPluginDescriptor.CALLBACK;
27+
28+
final class RedirectUriUtil
29+
{
30+
private RedirectUriUtil()
31+
{
32+
}
33+
34+
static String createRedirectUri(AuthenticatorInformationProvider authenticatorInformationProvider, ExceptionFactory exceptionFactory)
35+
{
36+
try
37+
{
38+
var authUri = authenticatorInformationProvider.getFullyQualifiedAuthenticationUri();
39+
40+
return new URL(authUri.toURL(), authUri.getPath() + "/" + CALLBACK).toString();
41+
}
42+
catch (MalformedURLException e)
43+
{
44+
throw exceptionFactory.internalServerException(ErrorCode.INVALID_REDIRECT_URI,
45+
"Could not create redirect URI");
46+
}
47+
}
48+
}

src/main/java/io/curity/identityserver/plugin/stackexchange/authentication/StackExchangeAuthenticatorRequestHandler.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Set;
4242
import java.util.UUID;
4343

44+
import static io.curity.identityserver.plugin.stackexchange.authentication.RedirectUriUtil.createRedirectUri;
4445
import static io.curity.identityserver.plugin.stackexchange.descriptor.StackExchangeAuthenticatorPluginDescriptor.CALLBACK;
4546
import static se.curity.identityserver.sdk.http.RedirectStatusCode.MOVED_TEMPORARILY;
4647

@@ -65,7 +66,7 @@ public Optional<AuthenticationResult> get(Request request, Response response)
6566
{
6667
_logger.info("GET request received for authentication authentication");
6768

68-
String redirectUri = createRedirectUri();
69+
String redirectUri = createRedirectUri(_authenticatorInformationProvider, _exceptionFactory);
6970
String state = UUID.randomUUID().toString();
7071
Map<String, Collection<String>> queryStringArguments = new LinkedHashMap<>(5);
7172
Set<String> scopes = new LinkedHashSet<>(4);
@@ -121,19 +122,4 @@ private static void addQueryString(Map<String, Collection<String>> queryStringAr
121122
{
122123
queryStringArguments.put(key, Collections.singleton(value.toString()));
123124
}
124-
125-
private String createRedirectUri()
126-
{
127-
try
128-
{
129-
URI authUri = _authenticatorInformationProvider.getFullyQualifiedAuthenticationUri();
130-
131-
return new URL(authUri.toURL(), authUri.getPath() + "/" + CALLBACK).toString();
132-
}
133-
catch (MalformedURLException e)
134-
{
135-
throw _exceptionFactory.internalServerException(ErrorCode.INVALID_REDIRECT_URI,
136-
"Could not create redirect URI");
137-
}
138-
}
139-
}
125+
}

0 commit comments

Comments
 (0)