Skip to content

Commit

Permalink
fix #4 and #5
Browse files Browse the repository at this point in the history
  • Loading branch information
greenlaw110 committed Apr 14, 2020
1 parent 0248be9 commit a906318
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# act-social-link CHANGE LOG

0.14.0
* update to act-1.8.32
* Google Social Provider - migrate profile fetch to the new people API #5
* It shall respond 403 if error encountered authenticate social provider or fetching profile #4

0.13.0 - 03/Nov/2019
* update to act-1.8.29

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<artifactId>act-social-link</artifactId>
<packaging>jar</packaging>
<version>0.13.1-SNAPSHOT</version>
<version>0.14.0-SNAPSHOT</version>

<name>ACT Social Link</name>
<description>Help act application to connect to social providers, e.g. facebook, linkedin etc</description>
Expand All @@ -35,7 +35,7 @@
<parent>
<groupId>org.actframework</groupId>
<artifactId>parent</artifactId>
<version>1.8.29</version>
<version>1.8.32</version>
</parent>

<properties>
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/act/social/SocialLink.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package act.social;
package act.social;

/*-
* #%L
Expand All @@ -24,6 +24,9 @@
import act.app.conf.AutoConfig;
import act.controller.Controller;
import act.event.EventBus;
import act.util.LogSupport;
import act.view.ActForbidden;
import act.view.ActUnauthorized;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.osgl.$;
Expand All @@ -34,9 +37,11 @@
import org.osgl.util.*;
import osgl.version.Version;

import static act.controller.Controller.Util.redirect;

@Controller("social")
@AutoConfig("social_link")
public class SocialLink extends Controller.Util {
public class SocialLink extends LogSupport {

public static final Version VERSION = Version.of(SocialLink.class);

Expand All @@ -52,10 +57,7 @@ public Result startSocialLink(
String payload,
ActionContext context
) {
if (null == callback) {
callback = context.req().referrer();
}
return redirect(provider.authUrl(callback, payload));
return redirect(socialRedirectLink(provider, callback, payload, context));
}

/**
Expand All @@ -67,7 +69,12 @@ public String socialRedirectLink(SocialProvider provider, String callback, Strin
if (null == callback) {
callback = context.req().referrer();
}
return provider.authUrl(callback, payload);
String redirectUrl = provider.authUrl(callback, payload);
if (isDebugEnabled()) {
debug("secure: %s\n\tprovider: %s\n\tcallback: %s\n\tpayload: %s\n\tredirect to: %s",
context.req().secure(), provider, callback, payload, redirectUrl);
}
return redirectUrl;
}

@Action(value = "callback", methods = {H.Method.GET, H.Method.POST})
Expand All @@ -77,6 +84,9 @@ public Result authCallback(
String state,
EventBus eventBus
) {
if (isDebugEnabled()) {
debug("callback >>>");
}
String act_callback = null;
String act_payload = null;
if (S.notBlank(state)) {
Expand All @@ -86,14 +96,25 @@ public Result authCallback(
act_payload = json.getString("act_payload");
}
try {
if (isDebugEnabled()) {
debug("checking CSRF token...");
}
provider.checkCsrfToken(state);
SocialProfile profile = provider.doAuth(code, act_callback, act_payload);
if (isDebugEnabled()) {
debug("profile fetched: %s", profile);
debug("triggering event");
}
// todo handle exception
eventBus.trigger(profile.createFetchedEvent(act_payload, provider.getId()));
if (isDebugEnabled()) {
debug("Fetch event triggered");
}
} catch (Result r) {
return r;
} catch (RuntimeException e) {
eventBus.trigger(new SocialLinkFailed());
eventBus.trigger(new SocialLinkFailed(e));
throw ActForbidden.create(e);
}
String originalCallback = act_callback;
if (S.blank(originalCallback)) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/act/social/SocialLinkFailed.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@

import act.event.ActEvent;

public class SocialLinkFailed extends ActEvent {
public class SocialLinkFailed extends ActEvent<RuntimeException> {

public SocialLinkFailed(RuntimeException cause) {
super(cause);
}
}
10 changes: 8 additions & 2 deletions src/main/java/act/social/SocialProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ protected static StringBuilder appendParam(StringBuilder sb, String key, String

protected static StringBuilder appendParams(StringBuilder sb, Map<String, String> args) {
String sep = sb.toString().contains("?") ? "&" : "?";
sb.append(sep).append("zz=0");
sb.append(sep);
boolean first = true;
for (Map.Entry<String, String> entry : args.entrySet()) {
String key = entry.getKey(), val = entry.getValue();
if (null != val) {
sb.append("&").append(key).append("=").append(Codec.encodeUrl(val));
if (!first) {
sb.append("&");
} else {
first = false;
}
sb.append(key).append("=").append(Codec.encodeUrl(val));
}
}
return sb;
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/act/social/provider/GoogleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import act.social.SocialId;
import act.social.SocialProfile;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.osgl.$;
import org.osgl.util.C;
Expand Down Expand Up @@ -71,16 +72,28 @@ public void fillProfile(SocialProfile user) {

String url = config.getProfileUrl();
Map<String, String> params = C.Map(
"fields", "emails/value,id,image/url,name(familyName,givenName)",
"personFields", "emailAddresses,names,photos",
authMethod.accessTokenParamName(), user.getToken()
);
JSONObject json = readUrlAsJson(url, params, false);
user.setId(new SocialId(json.getString("id"), this.getId()));
JSONObject name = json.getJSONObject("name");
user.setFirstName(name.getString("givenName"));
user.setLastName(name.getString("familyName"));
user.setEmail(json.getJSONArray("emails").getJSONObject(0).getString("value"));
user.setAvatarUrl(json.getJSONObject("image").getString("url"));
JSONArray emails = json.getJSONArray("emailAddresses");
if (!emails.isEmpty()) {
JSONObject emailObject = emails.getJSONObject(0);
user.setEmail(emailObject.getString("value"));
}
JSONArray names = json.getJSONArray("names");
if (!names.isEmpty()) {
JSONObject nameObject = names.getJSONObject(0);
user.setDisplayName(nameObject.getString("displayName"));
user.setFirstName(nameObject.getString("givenName"));
user.setLastName(nameObject.getString("familyName"));
}
JSONArray photos = json.getJSONArray("photos");
if (!photos.isEmpty()) {
JSONObject photoObject = photos.getJSONObject(0);
user.setAvatarUrl(photoObject.getString("url"));
}
if (isTraceEnabled()) {
trace("Done GoogleProvider.fillProfile");
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/social_link.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ social_link.github.profileUrl=https://api.github.com/user
#
social_link.google.requestTokenUrl=https://www.google.com/accounts/OAuthGetRequestToken
social_link.google.accessTokenUrl=https://www.googleapis.com/oauth2/v4/token
social_link.google.profileUrl=https://www.googleapis.com/plus/v1/people/me
#social_link.google.profileUrl=https://www.googleapis.com/plus/v1/people/me
social_link.google.profileUrl=https://people.googleapis.com/v1/people/me
social_link.google.authorizationUrl=https://accounts.google.com/o/oauth2/v2/auth
social_link.google.scope=openid email profile
#social_link.google.key=your_consumer_key
Expand Down

0 comments on commit a906318

Please sign in to comment.