Send an email on successful user registration. Use Gmail API for that. Add password confirmation and email to RegisterUserDialog.

This commit is contained in:
Me Car 2016-01-10 18:08:08 +09:00
parent 46f60cd857
commit a0ddd4fff0
11 changed files with 328 additions and 70 deletions

View file

@ -0,0 +1,86 @@
package mage.server;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Base64;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.Gmail.Builder;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.Message;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.mail.Session;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import mage.server.util.ConfigSettings;
import org.apache.log4j.Logger;
public class GmailClient {
private static final Logger logger = Logger.getLogger(Main.class);
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".store/xmage");
private static FileDataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static Credential credential;
public static boolean initilize() {
try {
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader("client_secrets.json"));
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
logger.error("client_secrets.json not found");
return false;
}
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(GmailScopes.GMAIL_COMPOSE)).setDataStoreFactory(
dataStoreFactory).build();
credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
return true;
} catch (IOException | GeneralSecurityException ex) {
logger.error("Error initializing GmailClient", ex);
}
return false;
}
public static boolean sendMessage(String email, String subject, String text) {
try {
Gmail gmail = new Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("XMage Server").build();
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email));
mimeMessage.setSubject(subject);
mimeMessage.setText(text);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
Message message = new Message();
message.setRaw(Base64.encodeBase64URLSafeString(baos.toByteArray()));
gmail.users().messages().send(ConfigSettings.getInstance().getGoogleAccount() + "@gmail.com", message).execute();
return true;
} catch (MessagingException | IOException ex) {
logger.error("Error sending message", ex);
}
return false;
}
}

View file

@ -107,7 +107,18 @@ public class Main {
fastDbMode = Boolean.valueOf(arg.replace(fastDBModeArg, ""));
}
}
ConfigSettings config = ConfigSettings.getInstance();
if (config.isAuthenticationActivated()) {
logger.info("Initializing GmailClient. This will open up a tab in your browser to ask for an OAuth access token.");
if (GmailClient.initilize()) {
logger.info("GmailClient initilized successfully.");
} else {
logger.fatal("GmailClient initialization failed.");
return;
}
}
logger.info("Loading cards...");
if (fastDbMode) {
CardScanner.scanned = true;
@ -117,7 +128,6 @@ public class Main {
logger.info("Done.");
deleteSavedGames();
ConfigSettings config = ConfigSettings.getInstance();
for (GamePlugin plugin: config.getGameTypes()) {
GameFactory.getInstance().addGameType(plugin.getName(), loadGameType(plugin), loadPlugin(plugin));
}

View file

@ -87,6 +87,12 @@ public class Session {
return returnMessage;
}
AuthorizedUserRepository.instance.add(userName, password, email);
if (GmailClient.sendMessage(email, "XMage Registration Completed",
"You are successfully registered as " + userName + ".")) {
logger.info("Sent a registration confirmation email to " + email + " for " + userName);
} else {
logger.error("Failed sending a registration confirmation email to " + email + " for " + userName);
}
return null;
}
}

View file

@ -94,7 +94,7 @@ public class ConfigSettings {
public int getBacklogSize() {
return config.getServer().getBacklogSize().intValue();
}
public int getMaxGameThreads() {
return config.getServer().getMaxGameThreads().intValue();
}
@ -114,14 +114,22 @@ public class ConfigSettings {
public String getUserNamePattern() {
return config.getServer().getUserNamePattern();
}
public String getMaxAiOpponents() {
return config.getServer().getMaxAiOpponents();
}
public Boolean isSaveGameActivated() {
return config.getServer().isSaveGameActivated();
}
return config.getServer().isSaveGameActivated();
}
public Boolean isAuthenticationActivated() {
return config.getServer().isAuthenticationActivated();
}
public String getGoogleAccount() {
return config.getServer().getGoogleAccount();
}
public List<Plugin> getPlayerTypes() {
return config.getPlayerTypes().getPlayerType();