Skip to content
This repository was archived by the owner on Mar 10, 2019. It is now read-only.

Commit f4b3d95

Browse files
committed
Merge branch 'develop'
2 parents c7e90f3 + cb0ec94 commit f4b3d95

9 files changed

Lines changed: 151 additions & 48 deletions

File tree

WebContent/index.js

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class LoginManager {
5858

5959
this.pyxEmail = this._pyx.find('input[type=email]');
6060

61-
this.pyxSubmit = this._pyx.find('._register');
62-
this.pyxSubmit.on('click', () => this.createPyxAccount());
61+
this.pyxRegister = this._pyx.find('._register');
62+
this.pyxRegister.on('click', () => this.createPyxAccount());
6363

6464
this._socials = $('#socialLogin');
6565
this.googleSignIn = this._socials.find('#googleSignIn');
@@ -148,47 +148,67 @@ class LoginManager {
148148
})
149149
}
150150

151-
setupGoogle() {
152-
gapi.load('auth2', () => {
153-
this.google_auth2 = gapi.auth2.init({
154-
client_id: '464424051073-loi5kmcc3qev5a1lr5hvrjiqb3kcu2cl.apps.googleusercontent.com',
155-
cookiepolicy: 'single_host_origin',
156-
});
151+
setupGoogle(appId) {
152+
if (appId === undefined) {
153+
this.googleSignIn.hide();
154+
} else {
155+
this.googleSignIn.show();
156+
gapi.load('auth2', () => {
157+
this.google_auth2 = gapi.auth2.init({
158+
client_id: appId,
159+
cookiepolicy: 'single_host_origin',
160+
});
157161

158-
this.google_auth2.attachClickHandler(this.googleSignIn[0], {},
159-
(user) => this._googleSuccess(user),
160-
(error) => Notifier.error("Failed signing in with Google.", error));
161-
});
162+
this.google_auth2.attachClickHandler(this.googleSignIn[0], {},
163+
(user) => this._googleSuccess(user),
164+
(error) => Notifier.error("Failed signing in with Google.", error));
165+
});
166+
}
162167
}
163168

164-
setupFacebook() {
165-
FB.init({
166-
appId: '198497240899387',
167-
cookie: true,
168-
xfbml: true,
169-
autoLogAppEvents: Notifier._debug,
170-
version: 'v2.12'
171-
});
169+
setupFacebook(appId) {
170+
if (appId === undefined) {
171+
this.facebookSignIn.hide();
172+
} else {
173+
FB.init({
174+
appId: appId,
175+
cookie: true,
176+
xfbml: true,
177+
autoLogAppEvents: Notifier._debug,
178+
version: 'v2.12'
179+
});
172180

173-
FB.AppEvents.logPageView();
181+
FB.AppEvents.logPageView();
174182

175-
this.facebookSignIn.on('click', () => FB.login((user) => {
176-
this._facebookSuccess(user);
177-
}, {
178-
scope: 'email,public_profile',
179-
}));
183+
this.facebookSignIn.show();
184+
this.facebookSignIn.on('click', () => FB.login((user) => {
185+
this._facebookSuccess(user);
186+
}, {
187+
scope: 'email,public_profile',
188+
}));
189+
}
180190
}
181191

182-
setupGitHub() {
183-
this.githubSignIn.on('click', () => {
184-
window.location = "https://github.com/login/oauth/authorize?scope=read:user,user:email&client_id=d7057c607cf69f592239"
185-
})
192+
setupGitHub(appId) {
193+
if (appId === undefined) {
194+
this.githubSignIn.hide();
195+
} else {
196+
this.githubSignIn.show();
197+
this.githubSignIn.on('click', () => {
198+
window.location = "https://github.com/login/oauth/authorize?scope=read:user,user:email&client_id=" + appId
199+
})
200+
}
186201
}
187202

188-
setupTwitter() {
189-
this.twitterSignIn.on('click', () => {
190-
window.location = "/TwitterStartAuthFlow"
191-
})
203+
setupTwitter(appId) {
204+
if (appId === undefined) {
205+
this.twitterSignIn.hide();
206+
} else {
207+
this.twitterSignIn.show();
208+
this.twitterSignIn.on('click', () => {
209+
window.location = "/TwitterStartAuthFlow"
210+
})
211+
}
192212
}
193213

194214
_showNickDialog(params, success) {
@@ -354,6 +374,14 @@ class LoginManager {
354374

355375
setup() {
356376
Requester.request("fl", {}, (data) => {
377+
/**
378+
* @param {object} data.aC - Auth config
379+
* @param {string} data.aC.g - Google app id
380+
* @param {string} data.aC.fb - Facebook app id
381+
* @param {string} data.aC.gh - Github app id
382+
* @param {string} data.aC.tw - Twitter app id
383+
* @param {string} data.aC.pw - Verification email sender
384+
*/
357385
data.css.sort(function (a, b) {
358386
return a.w - b.w;
359387
});
@@ -368,10 +396,22 @@ class LoginManager {
368396
window.location = "/games/";
369397
}
370398
} else {
371-
this.setupGoogle();
372-
this.setupFacebook();
373-
this.setupGitHub();
374-
this.setupTwitter();
399+
const authConfig = data.aC;
400+
Notifier.debug(authConfig);
401+
402+
403+
this.setupGoogle(authConfig.g);
404+
this.setupFacebook(authConfig.fb);
405+
this.setupGitHub(authConfig.gh);
406+
this.setupTwitter(authConfig.tw);
407+
408+
if ("pw" in authConfig) {
409+
this.pyxEmail.parent().show();
410+
this.pyxRegister.show();
411+
} else {
412+
this.pyxEmail.parent().hide();
413+
this.pyxRegister.hide();
414+
}
375415

376416
switch (getURLParameter("aT")) {
377417
case "gh":

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.gianlu.pyxreloaded</groupId>
77
<artifactId>server</artifactId>
8-
<version>0.3.0</version>
8+
<version>0.3.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>pyx-reloaded</name>

src/main/java/com/gianlu/pyxreloaded/Consts.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,11 @@ public enum GeneralKeys implements ReceivableKey, ReturnableKey {
706706
/**
707707
* User account data.
708708
*/
709-
ACCOUNT("a");
709+
ACCOUNT("a"),
710+
/**
711+
* Authentication configuration for client.
712+
*/
713+
AUTH_CONFIG("aC");
710714

711715
private final String key;
712716

@@ -1133,7 +1137,7 @@ public String toString() {
11331137
/**
11341138
* Identify auth type in database, also used to send authentication data
11351139
*/
1136-
public enum AuthType implements ReceivableKey {
1140+
public enum AuthType implements ReceivableKey, ReturnableKey {
11371141
@IgnoreDuplicateIn(GameOptionsData.class)
11381142
PASSWORD("pw"),
11391143
GOOGLE("g"),

src/main/java/com/gianlu/pyxreloaded/handlers/FirstLoadHandler.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import com.gianlu.pyxreloaded.game.GameOptions;
88
import com.gianlu.pyxreloaded.server.Annotations;
99
import com.gianlu.pyxreloaded.server.Parameters;
10+
import com.gianlu.pyxreloaded.singletons.Emails;
1011
import com.gianlu.pyxreloaded.singletons.LoadedCards;
1112
import com.gianlu.pyxreloaded.singletons.Preferences;
13+
import com.gianlu.pyxreloaded.singletons.SocialLogin;
1214
import com.google.gson.JsonArray;
1315
import io.undertow.server.HttpServerExchange;
1416

@@ -17,10 +19,17 @@
1719
public class FirstLoadHandler extends BaseHandler {
1820
public static final String OP = Consts.Operation.FIRST_LOAD.toString();
1921
private final LoadedCards loadedCards;
22+
private final Emails emails;
23+
private final SocialLogin socials;
2024
private final Preferences preferences;
2125

22-
public FirstLoadHandler(@Annotations.LoadedCards LoadedCards loadedCards, @Annotations.Preferences Preferences preferences) {
26+
public FirstLoadHandler(@Annotations.LoadedCards LoadedCards loadedCards,
27+
@Annotations.Emails Emails emails,
28+
@Annotations.SocialLogin SocialLogin socials,
29+
@Annotations.Preferences Preferences preferences) {
2330
this.loadedCards = loadedCards;
31+
this.emails = emails;
32+
this.socials = socials;
2433
this.preferences = preferences;
2534
}
2635

@@ -45,6 +54,14 @@ public JsonWrapper handle(User user, Parameters params, HttpServerExchange excha
4554
}
4655
}
4756

57+
JsonWrapper authConfig = new JsonWrapper();
58+
if (emails.enabled()) authConfig.add(Consts.AuthType.PASSWORD, emails.senderEmail());
59+
if (socials.googleEnabled()) authConfig.add(Consts.AuthType.GOOGLE, socials.googleAppId());
60+
if (socials.facebookEnabled()) authConfig.add(Consts.AuthType.FACEBOOK, socials.facebookAppId());
61+
if (socials.githubEnabled()) authConfig.add(Consts.AuthType.GITHUB, socials.githubAppId());
62+
if (socials.twitterEnabled()) authConfig.add(Consts.AuthType.TWITTER, socials.twitterAppId());
63+
obj.add(Consts.GeneralKeys.AUTH_CONFIG, authConfig);
64+
4865
Set<PyxCardSet> cardSets = loadedCards.getLoadedSets();
4966
JsonArray json = new JsonArray(cardSets.size());
5067
for (PyxCardSet cardSet : cardSets) json.add(cardSet.getClientMetadataJson().obj());

src/main/java/com/gianlu/pyxreloaded/singletons/Emails.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public Emails(ServerDatabase db, Preferences preferences, UsersWithAccount accou
4747
}
4848
}
4949

50+
@NotNull
51+
public String senderEmail() {
52+
return senderEmail;
53+
}
54+
5055
@Nullable
5156
private Mailer mailer(Preferences preferences) {
5257
if (senderEmail == null || senderEmail.isEmpty()) return null;

src/main/java/com/gianlu/pyxreloaded/singletons/SocialLogin.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ public final class SocialLogin {
3333
private final TwitterAuthHelper twitterHelper;
3434
private final GoogleIdTokenVerifier googleHelper;
3535
private final FacebookAuthHelper facebookHelper;
36+
private final String googleAppId;
3637

3738
public SocialLogin(@Nullable GithubAuthHelper githubHelper, @Nullable TwitterAuthHelper twitterHelper, @Nullable FacebookAuthHelper facebookHelper, Preferences preferences) {
3839
this.githubHelper = githubHelper;
3940
this.twitterHelper = twitterHelper;
4041
this.facebookHelper = facebookHelper;
41-
this.googleHelper = instantiateGoogleHelper(preferences);
42+
43+
this.googleAppId = preferences.getString("socials/googleClientId", null);
44+
this.googleHelper = instantiateGoogleHelper(googleAppId);
4245
}
4346

47+
@Contract("null -> null")
4448
@Nullable
45-
private static GoogleIdTokenVerifier instantiateGoogleHelper(Preferences preferences) {
46-
String clientId = preferences.getString("socials/googleClientId", null);
47-
if (clientId == null || clientId.isEmpty()) return null;
48-
49+
private static GoogleIdTokenVerifier instantiateGoogleHelper(String appId) {
50+
if (appId == null || appId.isEmpty()) return null;
4951
return new GoogleIdTokenVerifier.Builder(new ApacheHttpTransport(), new JacksonFactory())
50-
.setAudience(Collections.singletonList(clientId))
52+
.setAudience(Collections.singletonList(appId))
5153
.build();
5254
}
5355

@@ -124,18 +126,38 @@ public boolean googleEnabled() {
124126
return googleHelper != null;
125127
}
126128

129+
@Nullable
130+
public String googleAppId() {
131+
return googleAppId;
132+
}
133+
127134
@Contract(pure = true)
128135
public boolean facebookEnabled() {
129136
return facebookHelper != null;
130137
}
131138

139+
@Nullable
140+
public String facebookAppId() {
141+
return facebookHelper == null ? null : facebookHelper.appId();
142+
}
143+
132144
@Contract(pure = true)
133145
public boolean githubEnabled() {
134146
return githubHelper != null;
135147
}
136148

149+
@Nullable
150+
public String githubAppId() {
151+
return githubHelper == null ? null : githubHelper.appId();
152+
}
153+
137154
@Contract(pure = true)
138155
public boolean twitterEnabled() {
139156
return twitterHelper != null;
140157
}
158+
159+
@Nullable
160+
public String twitterAppId() {
161+
return twitterHelper == null ? null : twitterHelper.appId();
162+
}
141163
}

src/main/java/com/gianlu/pyxreloaded/socials/facebook/FacebookAuthHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public static FacebookAuthHelper instantiate(Preferences preferences) {
4141
return new FacebookAuthHelper(appId, appSecret);
4242
}
4343

44+
@NotNull
45+
public String appId() {
46+
return appId;
47+
}
48+
4449
@Nullable
4550
public FacebookToken verify(@NotNull String accessToken) throws IOException, FacebookOAuthException {
4651
HttpGet get = new HttpGet(DEBUG_TOKEN + "?input_token=" + accessToken + "&access_token=" + appToken);

src/main/java/com/gianlu/pyxreloaded/socials/github/GithubAuthHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,9 @@ public String exchangeCode(@NotNull String code) throws IOException, GithubExcep
130130
post.releaseConnection();
131131
}
132132
}
133+
134+
@NotNull
135+
public String appId() {
136+
return appId;
137+
}
133138
}

src/main/java/com/gianlu/pyxreloaded/socials/twitter/TwitterAuthHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public TwitterProfileInfo info(OAuth1AccessToken token) throws InterruptedExcept
8383
}
8484
}
8585

86+
@NotNull
87+
public String appId() {
88+
return service.getConfig().getApiKey();
89+
}
90+
8691
private static class HttpClientWrapper implements HttpClient {
8792
private final CloseableHttpClient client;
8893

0 commit comments

Comments
 (0)