Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

Commit

Permalink
Migrate to play 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
KadekM committed Jul 9, 2017
1 parent c098ffc commit b48e77b
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: scala
sudo: true
scala:
- 2.11.8
- 2.11.11
- 2.12.2
jdk:
- oraclejdk8
install: true
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ This plugin uses concepts from [securesocial2][] and [Play20StartApp][] and prov

Play Authenticate is cross-tested in Java 1.6, Java 1.7 (Up to `0.6.x`) and Java 1.8 (from `0.7.0`)

Since Play 2.6 it's cross-compiled for both Scala 2.11 and 2.12.

Works fine with Play version

* `2.0.2` to `2.0.x` (last: `0.2.3-SNAPSHOT` - [2.0.x branch](https://github.com/joscha/play-authenticate/tree/2.0.x))
* `2.1.0` to `2.1.x` (last: `0.3.6` - [2.1.x branch](https://github.com/joscha/play-authenticate/tree/2.1.x))
* `2.2.0` to `2.2.x` (last: `0.5.4` - [2.2.x branch](https://github.com/joscha/play-authenticate/tree/2.2.x))
* `2.3.0` to `2.3.x` (last: `0.6.9` - [2.3.x branch](https://github.com/joscha/play-authenticate/tree/2.3.x))
* `2.4.0` to `2.4.x` (last: `0.7.x` - [2.4.x branch](https://github.com/joscha/play-authenticate/tree/2.4.x))
* `2.5.0` to `2.5.x` (last: `0.8.x` - [master branch](https://github.com/joscha/play-authenticate/tree/master))
* `2.5.0` to `2.5.x` (last: `0.8.x` - [2.5.x branch](https://github.com/joscha/play-authenticate/tree/2.5.x))
* `2.6.0` to `2.6.x` (last: `0.9.x` - [master branch](https://github.com/joscha/play-authenticate/tree/master))

Releases are on [mvnrepository](http://mvnrepository.com/artifact/com.feth) and snapshots can be found on [sonatype](https://oss.sonatype.org/content/repositories/snapshots/com/feth/).

Expand All @@ -32,14 +35,14 @@ Play-Authenticate is available in [Maven Central](http://search.maven.org/#brows
<dependency>
<groupId>com.feth</groupId>
<artifactId>play-authenticate_2.11</artifactId>
<version>0.8.3</version>
<version>0.9.0</version>
</dependency>
```
or

```scala
val appDependencies = Seq(
"com.feth" % "play-authenticate_2.11" % "0.8.3"
"com.feth" %% "play-authenticate" % "0.9.0"
)
```

Expand Down
46 changes: 29 additions & 17 deletions code/app/com/feth/play/module/pa/PlayAuthenticate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.feth.play.module.pa.providers.AuthProvider;
import com.feth.play.module.pa.service.UserService;
import com.feth.play.module.pa.user.AuthUser;
import play.Configuration;
import com.typesafe.config.Config;
import play.Logger;
import play.i18n.Messages;
import play.cache.SyncCacheApi;
import play.i18n.Lang;
import play.i18n.MessagesApi;
import play.mvc.Call;
import play.mvc.Controller;
import play.mvc.Http;
Expand All @@ -16,7 +18,10 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Singleton
public class PlayAuthenticate {
Expand All @@ -28,17 +33,24 @@ public class PlayAuthenticate {
private static final String SETTING_KEY_ACCOUNT_AUTO_LINK = "accountAutoLink";
private static final String SETTING_KEY_ACCOUNT_AUTO_MERGE = "accountAutoMerge";



private Configuration config;
private List<Lang> prefferedLangs;
private Config config;

@Inject
public PlayAuthenticate(final Configuration config, final Resolver resolver) {
public PlayAuthenticate(final Config config, final Resolver resolver, final MessagesApi messagesApi, final SyncCacheApi cacheApi) {
this.config = config;
this.resolver = resolver;
this.messagesApi = messagesApi;
this.cacheApi = cacheApi;

Locale englishLocale = new Locale("en");
Lang englishLang = new Lang(englishLocale);
prefferedLangs = Arrays.asList(englishLang);
}

private Resolver resolver;
private final MessagesApi messagesApi;
private final SyncCacheApi cacheApi;

public Resolver getResolver() {
return resolver;
Expand All @@ -53,7 +65,7 @@ public void setUserService(final UserService service) {
public UserService getUserService() {
if (userService == null) {
throw new RuntimeException(
Messages.get("playauthenticate.core.exception.no_user_service"));
messagesApi.preferred(prefferedLangs).at("playauthenticate.core.exception.no_user_service"));
}
return userService;
}
Expand All @@ -64,7 +76,7 @@ public UserService getUserService() {
private static final String EXPIRES_KEY = "pa.u.exp";
private static final String SESSION_ID_KEY = "pa.s.id";

public Configuration getConfiguration() {
public Config getConfiguration() {
return config
.getConfig(SETTING_KEY_PLAY_AUTHENTICATE);
}
Expand Down Expand Up @@ -211,14 +223,14 @@ private void storeUserInCache(final Session session,

public void storeInCache(final Session session, final String key,
final Object o) {
play.cache.Cache.set(getCacheKey(session, key), o);
cacheApi.set(getCacheKey(session, key), o);
}

public <T> T removeFromCache(final Session session, final String key) {
final T o = getFromCache(session, key);

final String k = getCacheKey(session, key);
play.cache.Cache.remove(k);
cacheApi.remove(k);
return o;
}

Expand All @@ -229,7 +241,7 @@ private String getCacheKey(final Session session, final String key) {

@SuppressWarnings("unchecked")
public <T> T getFromCache(final Session session, final String key) {
return (T) play.cache.Cache.get(getCacheKey(session, key));
return (T) cacheApi.get(getCacheKey(session, key));
}

private AuthUser getUserFromCache(final Session session,
Expand Down Expand Up @@ -356,7 +368,7 @@ private AuthUser signupUser(final AuthUser u, final Session session, final AuthP
final Object id = getUserService().save(u);
if (id == null) {
throw new AuthException(
Messages.get("playauthenticate.core.exception.signupuser_failed"));
messagesApi.preferred(prefferedLangs).at("playauthenticate.core.exception.signupuser_failed"));
}
provider.afterSave(u, id, session);
return u;
Expand All @@ -368,7 +380,7 @@ public Result handleAuthentication(final String provider,
if (ap == null) {
// Provider wasn't found and/or user was fooling with our stuff -
// tell him off:
return Controller.notFound(Messages.get(
return Controller.notFound(messagesApi.preferred(prefferedLangs).at(
"playauthenticate.core.exception.provider_not_found",
provider));
}
Expand Down Expand Up @@ -452,7 +464,7 @@ public Result handleAuthentication(final String provider,
final Call c = getResolver().askMerge();
if (c == null) {
throw new RuntimeException(
Messages.get(
messagesApi.preferred(prefferedLangs).at(
"playauthenticate.core.exception.merge.controller_undefined",
SETTING_KEY_ACCOUNT_AUTO_MERGE));
}
Expand Down Expand Up @@ -484,7 +496,7 @@ public Result handleAuthentication(final String provider,
final Call c = getResolver().askLink();
if (c == null) {
throw new RuntimeException(
Messages.get(
messagesApi.preferred(prefferedLangs).at(
"playauthenticate.core.exception.link.controller_undefined",
SETTING_KEY_ACCOUNT_AUTO_LINK));
}
Expand All @@ -496,8 +508,8 @@ public Result handleAuthentication(final String provider,

return loginAndRedirect(context, loginUser);
} else {
return Controller.internalServerError(Messages
.get("playauthenticate.core.exception.general"));
return Controller.internalServerError(messagesApi
.preferred(prefferedLangs).at("playauthenticate.core.exception.general"));
}
} catch (final AuthException e) {
final Call c = getResolver().onException(e);
Expand Down
6 changes: 3 additions & 3 deletions code/app/com/feth/play/module/pa/providers/AuthProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.feth.play.module.pa.exceptions.AuthException;
import com.feth.play.module.pa.user.AuthUser;
import com.feth.play.module.pa.user.SessionAuthUser;
import play.Configuration;
import com.typesafe.config.Config;
import play.Logger;
import play.inject.ApplicationLifecycle;
import play.mvc.Http.Context;
Expand Down Expand Up @@ -67,7 +67,7 @@ protected void onStart() {

final List<String> neededSettings = neededSettingKeys();
if (neededSettings != null) {
final Configuration c = getConfiguration();
final Config c = getConfiguration();
if (c == null) {
throw new RuntimeException("No settings for provider '"
+ getKey() + "' available at all!");
Expand Down Expand Up @@ -100,7 +100,7 @@ protected String getAbsoluteUrl(final Request request) {

public abstract String getKey();

protected Configuration getConfiguration() {
protected Config getConfiguration() {
return this.auth.getConfiguration().getConfig(getKey());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected List<String> neededSettingKeys() {
}

protected long getTimeout() {
return getConfiguration().getLong(SettingKeys.TIMEOUT, PlayAuthenticate.TIMEOUT);
return getConfiguration().getLong(SettingKeys.TIMEOUT);
}

private boolean useSecureRedirectUri() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.feth.play.module.pa.exceptions.AuthException;
import com.feth.play.module.pa.providers.ext.ExternalAuthProvider;
import com.feth.play.module.pa.user.AuthUserIdentity;
import play.Configuration;
import com.typesafe.config.Config;
import play.Logger;
import play.inject.ApplicationLifecycle;
import play.libs.oauth.OAuth;
Expand Down Expand Up @@ -103,7 +103,7 @@ public Object authenticate(final Context context, final Object payload)
Logger.debug("Returned with URL: '" + uri + "'");
}

final Configuration c = getConfiguration();
final Config c = getConfiguration();

final ConsumerKey key = new ConsumerKey(
c.getString(SettingKeys.CONSUMER_KEY),
Expand Down Expand Up @@ -170,7 +170,7 @@ protected JsonNode signedOauthGet(final String url, final OAuthCalculator calcul
protected OAuthCalculator getOAuthCalculator(final OAuth1AuthInfo info) {
final RequestToken token = new RequestToken(info.getAccessToken(),
info.getAccessTokenSecret());
final Configuration c = getConfiguration();
final Config c = getConfiguration();
final ConsumerKey cK = new ConsumerKey(
c.getString(SettingKeys.CONSUMER_KEY),
c.getString(SettingKeys.CONSUMER_SECRET));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import com.feth.play.module.pa.providers.ext.ExternalAuthProvider;
import com.feth.play.module.pa.user.AuthUser;
import com.feth.play.module.pa.user.AuthUserIdentity;
import com.typesafe.config.Config;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import play.Configuration;
import play.Logger;
import play.i18n.Messages;
import play.i18n.MessagesApi;
import play.inject.ApplicationLifecycle;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
Expand Down Expand Up @@ -42,10 +42,12 @@ public QueryParam(String param, String value) {
protected static final String CONTENT_TYPE = "Content-Type";

protected final WSClient wsClient;
private final MessagesApi messagesApi;

public OAuth2AuthProvider(final PlayAuthenticate auth, final ApplicationLifecycle lifecycle, final WSClient wsClient) {
public OAuth2AuthProvider(final PlayAuthenticate auth, final ApplicationLifecycle lifecycle, final WSClient wsClient, final MessagesApi messagesApi) {
super(auth, lifecycle);
this.wsClient = wsClient;
this.messagesApi = messagesApi;
}

@Override
Expand Down Expand Up @@ -94,7 +96,7 @@ protected WSResponse fetchAuthResponse(String url, QueryParam...params) throws A
final List<QueryParam> queryParams = Arrays.asList(params);
final WSRequest request = wsClient.url(url);
for(QueryParam param : queryParams) {
request.setQueryParameter(param.param, param.value);
request.addQueryParameter(param.param, param.value);
}

try {
Expand All @@ -106,7 +108,7 @@ protected WSResponse fetchAuthResponse(String url, QueryParam...params) throws A
}
}

protected String getAccessTokenParams(final Configuration c,
protected String getAccessTokenParams(final Config c,
final String code, Request request) throws ResolverMissingException {
final List<NameValuePair> params = getParams(request, c);
params.add(new BasicNameValuePair(Constants.CLIENT_SECRET, c
Expand All @@ -124,13 +126,13 @@ protected Map<String, String> getHeaders() {

protected I getAccessToken(final String code, final Request request)
throws AccessTokenException, ResolverMissingException {
final Configuration c = getConfiguration();
final Config c = getConfiguration();
final String params = getAccessTokenParams(c, code, request);
final String url = c.getString(SettingKeys.ACCESS_TOKEN_URL);
final WSRequest wrh = wsClient.url(url);
wrh.setHeader(CONTENT_TYPE, "application/x-www-form-urlencoded");
wrh.addHeader(CONTENT_TYPE, "application/x-www-form-urlencoded");
for(final Map.Entry<String, String> header : getHeaders().entrySet()) {
wrh.setHeader(header.getKey(), header.getValue());
wrh.addHeader(header.getKey(), header.getValue());
}

try {
Expand All @@ -146,12 +148,12 @@ protected abstract I buildInfo(final WSResponse r)

protected String getAuthUrl(final Request request, final String state)
throws AuthException {
final Configuration c = getConfiguration();
final Config c = getConfiguration();
final List<NameValuePair> params = getAuthParams(c, request, state);
return generateURI(c.getString(SettingKeys.AUTHORIZATION_URL), params);
}

protected List<NameValuePair> getAuthParams(final Configuration c,
protected List<NameValuePair> getAuthParams(final Config c,
final Request request, final String state) throws AuthException {
final List<NameValuePair> params = getParams(request, c);
if (c.getString(SettingKeys.SCOPE) != null) {
Expand Down Expand Up @@ -179,7 +181,7 @@ protected List<NameValuePair> getAuthParams(final Configuration c,
}

protected List<NameValuePair> getParams(final Request request,
final Configuration c) throws ResolverMissingException {
final Config c) throws ResolverMissingException {
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(Constants.CLIENT_ID, c
.getString(SettingKeys.CLIENT_ID)));
Expand Down Expand Up @@ -226,7 +228,7 @@ public Object authenticate(final Context context, final Object payload)
final String callbackState = request.getQueryString(Constants.STATE);
if(!storedState.equals(UUID.fromString(callbackState))) {
// the return callback may have been forged
throw new AuthException(Messages.get("playauthenticate.core.exception.oauth2.state_param_forged"));
throw new AuthException(messagesApi.preferred(request).at("playauthenticate.core.exception.oauth2.state_param_forged"));
}
final String code = request.getQueryString(Constants.CODE);
final I info = getAccessToken(code, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.feth.play.module.pa.exceptions.AuthException;
import com.feth.play.module.pa.providers.oauth2.OAuth2AuthProvider;
import play.Logger;
import play.i18n.MessagesApi;
import play.inject.ApplicationLifecycle;
import play.libs.ws.WSClient;
import play.libs.ws.WSResponse;
Expand All @@ -27,8 +28,8 @@ public class EventBriteAuthProvider extends
private static final String TOKEN = "token";

@Inject
public EventBriteAuthProvider(final PlayAuthenticate auth, final ApplicationLifecycle lifecycle, final WSClient wsClient) {
super(auth, lifecycle, wsClient);
public EventBriteAuthProvider(final PlayAuthenticate auth, final ApplicationLifecycle lifecycle, final WSClient wsClient, final MessagesApi messagesApi) {
super(auth, lifecycle, wsClient, messagesApi);
}


Expand Down
Loading

0 comments on commit b48e77b

Please sign in to comment.