Skip to content

Commit

Permalink
(#35) Google profiili pärimise url muudetud
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanhk committed Dec 30, 2018
1 parent b842ffe commit d8edded
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/main/java/server/security/DatabaseAuthorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.oauth.profile.facebook.FacebookProfile;
import org.pac4j.oauth.profile.google2.Google2Profile;
import server.entity.TriFunction;
import server.service.DatabaseService;

Expand Down Expand Up @@ -45,7 +44,7 @@ public DatabaseAuthorizer(DatabaseService database) {
* Checks whether Pac4j user profile is authorized.
*/
@Override
protected boolean isProfileAuthorized(WebContext context, CommonProfile profile) throws HttpAction {
protected boolean isProfileAuthorized(WebContext context, CommonProfile profile) {
return profile != null && ProfileAuthorizer.isAuthorized(database, profile,
getRows(database.getAllUsers().rxSetHandler().toBlocking().value()));
}
Expand All @@ -69,7 +68,7 @@ protected boolean handleError(WebContext context) throws HttpAction {
*/
public enum ProfileAuthorizer {
FACEBOOK(FacebookProfile.class, oAuth2Authorization()),
GOOGLE(Google2Profile.class, oAuth2Authorization()),
GOOGLE(GoogleProfile.class, oAuth2Authorization()),
IDCARD(IdCardProfile.class, (IdCardProfile p, Stream<JsonObject> stream, DatabaseService database) -> stream
.anyMatch(json -> p.getSerial().equals(json.getString(USERNAME.getName()))) ||
database.insertUser(p.getSerial(), genString(), p.getFirstName(), p.getFamilyName())
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/server/security/Google2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import org.pac4j.core.logout.GoogleLogoutActionBuilder;
import org.pac4j.oauth.client.OAuth20Client;
import org.pac4j.oauth.exception.OAuthCredentialsException;
import org.pac4j.oauth.profile.google2.Google2Profile;
import org.pac4j.oauth.profile.google2.Google2ProfileDefinition;

/**
* @author <a href="https://github.com/kristjanhk">Kristjan Hendrik Küngas</a>
*/
public class Google2Client extends OAuth20Client<Google2Profile> {
private static final String FULL_SCOPE = "https://www.googleapis.com/auth/plus.login email";
public class Google2Client extends OAuth20Client<GoogleProfile> {
private static final String FULL_SCOPE = "profile email";

public Google2Client(final String key, final String secret) {
setKey(key);
Expand All @@ -22,7 +20,7 @@ public Google2Client(final String key, final String secret) {
@Override
protected void clientInit(WebContext context) {
configuration.setApi(GoogleApi20.instance());
configuration.setProfileDefinition(new Google2ProfileDefinition());
configuration.setProfileDefinition(new GoogleProfileDefinition());
configuration.setScope(FULL_SCOPE);
configuration.setWithState(true);
configuration.setHasBeenCancelledFactory(ctx -> "access_denied"
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/server/security/GoogleProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package server.security;

import org.pac4j.oauth.profile.OAuth20Profile;

/**
* @author <a href="https://github.com/kristjanhk">Kristjan Hendrik Küngas</a>
*/
public class GoogleProfile extends OAuth20Profile {

@Override
public String getEmail() {
return getAttribute(GoogleProfileDefinition.EMAIL, String.class);
}

@Override
public String getFirstName() {
return getAttribute(GoogleProfileDefinition.GIVEN_NAME, String.class);
}

@Override
public String getFamilyName() {
return getAttribute(GoogleProfileDefinition.FAMILY_NAME, String.class);
}
}
36 changes: 36 additions & 0 deletions src/main/java/server/security/GoogleProfileDefinition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package server.security;

import com.github.scribejava.core.model.OAuth2AccessToken;
import io.vertx.core.json.JsonObject;
import org.pac4j.oauth.config.OAuth20Configuration;
import org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition;

import java.util.stream.Stream;

/**
* @author <a href="https://github.com/kristjanhk">Kristjan Hendrik Küngas</a>
*/
public class GoogleProfileDefinition extends OAuth20ProfileDefinition<GoogleProfile> {
public static final String ID = "sub";
public static final String EMAIL = "email";
public static final String GIVEN_NAME = "given_name";
public static final String FAMILY_NAME = "family_name";

public GoogleProfileDefinition() {
super(x -> new GoogleProfile());
}

@Override
public String getProfileUrl(OAuth2AccessToken accessToken, OAuth20Configuration configuration) {
return "https://openidconnect.googleapis.com/v1/userinfo";
}

@Override
public GoogleProfile extractUserProfile(String body) {
GoogleProfile profile = newProfile();
JsonObject json = new JsonObject(body);
profile.setId(json.getString(ID));
Stream.of(EMAIL, GIVEN_NAME, FAMILY_NAME).forEach(key -> convertAndAdd(profile, key, json.getString(key)));
return profile;
}
}

0 comments on commit d8edded

Please sign in to comment.