-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#35) Google profiili pärimise url muudetud
- Loading branch information
1 parent
b842ffe
commit d8edded
Showing
4 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
src/main/java/server/security/GoogleProfileDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |