Base forgot password + git keygen on register
This commit is contained in:
parent
9e2a4735d3
commit
24dc025507
3 changed files with 80 additions and 1 deletions
|
@ -35,6 +35,7 @@ import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
|
|||
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
|
||||
import be.jeffcheasey88.peeratcode.routes.users.ChangePassword;
|
||||
import be.jeffcheasey88.peeratcode.routes.users.ForgotPassword;
|
||||
import be.jeffcheasey88.peeratcode.routes.users.Login;
|
||||
import be.jeffcheasey88.peeratcode.routes.users.ProfileSettings;
|
||||
import be.jeffcheasey88.peeratcode.routes.users.Register;
|
||||
|
@ -79,6 +80,7 @@ public class Main{
|
|||
router.register(new Login(repo, router));
|
||||
router.register(new ProfileSettings(repo));
|
||||
router.register(new ChangePassword(repo));
|
||||
router.register(new ForgotPassword());
|
||||
|
||||
router.register(new ChapterElement(repo));
|
||||
router.register(new ChapterList(repo));
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package be.jeffcheasey88.peeratcode.routes.users;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
|
||||
public class ForgotPassword implements Response{
|
||||
|
||||
@Route(path = "^/user/fpw$")
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if(user != null){
|
||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -2,17 +2,27 @@ package be.jeffcheasey88.peeratcode.routes.users;
|
|||
|
||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
import java.util.Base64.Encoder;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
import org.json.simple.JSONAware;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
|
@ -24,11 +34,20 @@ public class Register extends FormResponse {
|
|||
private DatabaseRepository databaseRepo;
|
||||
private Router router;
|
||||
private String usersFilesPath;
|
||||
private KeyPairGenerator generator;
|
||||
private Encoder encoder;
|
||||
|
||||
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
try {
|
||||
generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(2048); //a changer ?
|
||||
} catch (NoSuchAlgorithmException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
encoder = Base64.getEncoder();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/register", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
|
@ -79,6 +98,40 @@ public class Register extends FormResponse {
|
|||
}
|
||||
}
|
||||
|
||||
private String generateGitKey(String email, String pseudo, String password) throws Exception{
|
||||
KeyPair pair = generator.generateKeyPair(); //doit être unique ???
|
||||
|
||||
JSONObject createUser = new JSONObject();
|
||||
createUser.put("email", email);
|
||||
createUser.put("username", pseudo);
|
||||
createUser.put("password", password);
|
||||
post("https://git-users.peerat.dev/api/v1/admin/users/", createUser);
|
||||
|
||||
JSONObject sendKey = new JSONObject();
|
||||
sendKey.put("key", new String(encoder.encode(pair.getPrivate().getEncoded()))); //add ssh-rsa au début ?
|
||||
sendKey.put("read_only", false);
|
||||
sendKey.put("title", "peer_at_code_auto_push_key_"+pseudo);
|
||||
post("https://git-users.peerat.dev/api/v1/admin/users/"+pseudo+"/keys", sendKey);
|
||||
|
||||
return new String(encoder.encode(pair.getPrivate().getEncoded()));
|
||||
}
|
||||
|
||||
private void post(String url, JSONAware json) throws Exception{
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type","application/json");
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
|
||||
writer.write(json.toJSONString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
int response = connection.getResponseCode();
|
||||
if(response != 201) throw new Exception("Call to "+url+" failed with response code "+response);
|
||||
}
|
||||
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
|
||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||
|
|
Loading…
Add table
Reference in a new issue