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

Commit

Permalink
Merge pull request #364 from joscha/play2.6-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha authored Aug 27, 2018
2 parents 0ff2b24 + 604c8fd commit 4dc8b41
Show file tree
Hide file tree
Showing 68 changed files with 406 additions and 390 deletions.
25 changes: 21 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
language: scala
sudo: true
sudo: required
dist: trusty
scala:
- 2.11.8
- 2.11.12
- 2.12.6
jdk:
- oraclejdk8
install: true
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
# See https://github.com/SeleniumHQ/docker-selenium/issues/87
- export DBUS_SESSION_BUS_ADDRESS=/dev/null
- export $(dbus-launch)
- export NSS_USE_SHARED_DB=ENABLED
- "export DISPLAY=:99.0"
- "Xvfb :99 +extension RANDR 2>/dev/null &"
- sleep 5 # give xvfb some time to start
- "mkdir -p ~/tmp/bin"
- "pushd ~/tmp/bin && wget -c https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-linux64.tar.gz && tar -xzvf geckodriver-v0.21.0-linux64.tar.gz && popd"
- "chmod -vv +x ~/tmp/bin/*"
- "export PATH=$HOME/tmp/bin:$PATH"
- wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
script:
- cd $TRAVIS_BUILD_DIR/code
Expand All @@ -25,6 +36,12 @@ script:
- sbt ++$TRAVIS_SCALA_VERSION test < /dev/null
notifications:
email: false
addons:
firefox: latest
apt:
packages:
- unzip
- dbus-x11
env:
global:
# SONATYPE_USERNAME
Expand Down
14 changes: 10 additions & 4 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 @@ -31,15 +34,15 @@ Play-Authenticate is available in [Maven Central](http://search.maven.org/#brows
```xml
<dependency>
<groupId>com.feth</groupId>
<artifactId>play-authenticate_2.11</artifactId>
<version>0.8.3</version>
<artifactId>play-authenticate</artifactId>
<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 Expand Up @@ -95,6 +98,9 @@ There is also a [sample application using Play!Authenticate with MongoDB](https:
## Versions
* **TRUNK** [not released in the repository, yet]
* Fancy contributing something? :-)
* **0.9.0** [tbd]
* Upgrade to Play 2.6 (thanks @KadekM)
* ATTENTION: This is for Play 2.6 - if you have Play 2.5, use a `0.8.x` version.
* Turkish core (thanks @canoztokmak)
* **0.8.3** [2017-04-18]
* Chinese translation (thanks @frederick036)
Expand Down
52 changes: 32 additions & 20 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> preferredLangs;
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);
preferredLangs = 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(preferredLangs).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 @@ -290,9 +302,9 @@ private String getUrl(final Call c, final String settingFallback) {
} else {
// go to root instead, but log this
Logger.warn("Resolver did not contain information about where to go - redirecting to /");
final String afterAuthFallback = getConfiguration().getString(
settingFallback);
if (afterAuthFallback != null && !afterAuthFallback.equals("")) {
final String afterAuthFallback;
if (getConfiguration().hasPath(settingFallback) && !(afterAuthFallback = getConfiguration().getString(
settingFallback)).isEmpty()) {
return afterAuthFallback;
}
// Not even the config setting was there or valid...meh
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(preferredLangs).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(preferredLangs).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(preferredLangs).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(preferredLangs).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(preferredLangs).at("playauthenticate.core.exception.general"));
}
} catch (final AuthException e) {
final Call c = getResolver().onException(e);
Expand Down
9 changes: 4 additions & 5 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,14 +67,13 @@ 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!");
}
for (final String key : neededSettings) {
final String setting = c.getString(key);
if (setting == null || "".equals(setting)) {
if (!c.hasPath(key) || c.getString(key).isEmpty()) {
throw new RuntimeException("Provider '" + getKey()
+ "' missing needed setting '" + key + "'");
}
Expand All @@ -100,7 +99,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 All @@ -57,16 +57,17 @@ protected String getRedirectUrl(final Request request,
}

protected String getRedirectUrl(final Request request) throws ResolverMissingException {
final String overrideHost = getConfiguration().getString(
SettingKeys.REDIRECT_URI_HOST);
final boolean isHttps = useSecureRedirectUri();
final Resolver resolver = this.auth.getResolver();
if (resolver == null) {
throw new ResolverMissingException("Resolver has not been set.");
}
final Call c = resolver.auth(getKey());
if (overrideHost != null && !overrideHost.trim().isEmpty()) {
return "http" + (isHttps ? "s" : "") + "://" + overrideHost

final String overrideHost;
if (getConfiguration().hasPath(SettingKeys.REDIRECT_URI_HOST)
&& !(overrideHost = getConfiguration().getString(SettingKeys.REDIRECT_URI_HOST)).trim().isEmpty()) {
return "http" + (isHttps ? "s" : "") + "://" + overrideHost
+ c.url();
} else {
return c.absoluteURL(request, isHttps);
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 @@ -107,7 +107,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 @@ -174,7 +174,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
@@ -1,18 +1,14 @@
package com.feth.play.module.pa.providers.oauth1.linkedin;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;

import com.feth.play.module.pa.providers.oauth1.BasicOAuth1AuthUser;
import com.feth.play.module.pa.providers.oauth1.OAuth1AuthInfo;
import com.feth.play.module.pa.user.BasicIdentity;
import com.feth.play.module.pa.user.EducationsIdentity;
import com.feth.play.module.pa.user.EmploymentsIdentity;
import com.feth.play.module.pa.user.FirstLastNameIdentity;
import com.feth.play.module.pa.user.PicturedIdentity;
import com.feth.play.module.pa.user.ProfiledIdentity;
import com.feth.play.module.pa.user.*;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LinkedinAuthUser extends BasicOAuth1AuthUser implements
BasicIdentity, FirstLastNameIdentity, PicturedIdentity,
Expand Down Expand Up @@ -104,7 +100,7 @@ public LinkedinAuthUser(final JsonNode nodeInfo, final String email,
}
}

private static EducationInfo makeEducation(final JsonNode node) {
private static EducationsIdentity.EducationInfo makeEducation(final JsonNode node) {
String id = null, schoolName = null, degree = null;
int startDateYear = 0, endDateYear = 0;
if (node.has(Constants.Education.ID)) {
Expand All @@ -129,7 +125,7 @@ private static EducationInfo makeEducation(final JsonNode node) {
endDateYear);
}

private static EmploymentInfo makeEmployment(final JsonNode node) {
private static EmploymentsIdentity.EmploymentInfo makeEmployment(final JsonNode node) {
String id = null, title = null, summary = null, companyName = null;
int startDateMonth = 0, startDateYear = 0, endDateMonth = 0, endDateYear = 0;
boolean isCurrent = false;
Expand Down
Loading

0 comments on commit 4dc8b41

Please sign in to comment.