diff --git a/src/main/java/org/ntlab/acanthus_server/entities/Account.java b/src/main/java/org/ntlab/acanthus_server/entities/Account.java index f8536ba..5ffe796 100644 --- a/src/main/java/org/ntlab/acanthus_server/entities/Account.java +++ b/src/main/java/org/ntlab/acanthus_server/entities/Account.java @@ -20,56 +20,68 @@ // //////////////////////////////////////////////////////////////////////////////////////// package org.ntlab.acanthus_server.entities; +import java.util.UUID; + public class Account { private int uid; private String name; private String email; private String token; private String password; - //コンストラクト + + //コンストラクト public Account(String name, String email, String password) { - this.name=name; - this.email=email; - this.password=password; + this.name = name; + this.email = email; + this.password = password; } + //----------------------------------------------------------------- + //----------------------------------------------------------------- //uidを返す //GetとSetを実装 public int getUid() { return uid; } - public void setUid(){ - this.uid=uid; + + //----------------------------------------------------------------- + public void setUid(int uid) { + this.uid = uid; } - - + + //----------------------------------------------------------------- //tokenを返す //GetとSetを実装 public String getToken() { return token; } - public void setToken(String token){ - this.token=token; + + //----------------------------------------------------------------- + // トークンを生成する + public void createToken() { + this.token = UUID.randomUUID().toString(); } + + //----------------------------------------------------------------- //passwordをtoken認証後返す public String getPassword(String token) { //IFでトークン認証をしたのちに返す return password; } + //----------------------------------------------------------------- //emailを返す public String getEmail() { return email; } - public void setEmail(String email){ - this.email=email; + //----------------------------------------------------------------- + public void setEmail(String email) { + this.email = email; } - - //Auth関数でpassword認証 - public boolean accountAuth(String password){ - return this.password == password; + //----------------------------------------------------------------- + // パスワードが一致しているかを判定 + public boolean isMatchedPassword(String password) { + return this.password.equals(password); } - - } diff --git a/src/main/java/org/ntlab/acanthus_server/models/Accounts.java b/src/main/java/org/ntlab/acanthus_server/models/Accounts.java index 69664be..befa65e 100644 --- a/src/main/java/org/ntlab/acanthus_server/models/Accounts.java +++ b/src/main/java/org/ntlab/acanthus_server/models/Accounts.java @@ -5,41 +5,41 @@ import java.util.HashMap; /* -* アカウント管理シングルトン -* -*/ + * アカウント管理シングルトン + * + */ public class Accounts { private static Accounts _theInstance = null; - /** - * @param Integer ユーザーID - * @param Account 対応するユーザー - */ private HashMap _accountHashMap = new HashMap<>(); //----------------------------------------------------------------- // インスタンス生成禁止 - private Accounts(){} + private Accounts() { + } //----------------------------------------------------------------- // シングルトン取得 //----------------------------------------------------------------- - public static Accounts getInstance(){ - if(_theInstance == null) _theInstance = new Accounts(); + public static Accounts getInstance() { + if (_theInstance == null) _theInstance = new Accounts(); return _theInstance; } + //----------------------------------------------------------------- //----------------------------------------------------------------- // Uidからアカウントを返す //----------------------------------------------------------------- - public Account getAccountByUid(int uid){ return _accountHashMap.get(uid); } + public Account getAccountByUid(int uid) { + return _accountHashMap.get(uid); + } //----------------------------------------------------------------- // e-Mailからアカウントを返す //----------------------------------------------------------------- - public Account getAccountByEMail(String email){ - for(var account : _accountHashMap.values()){ - // if(email == account.getEmail()) return account; + public Account getAccountByEMail(String email) { + for (var account : _accountHashMap.values()) { + if (account.getEmail().equals(email)) return account; } return null; } diff --git a/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java b/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java index 21ea92a..461387e 100644 --- a/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java +++ b/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java @@ -5,7 +5,6 @@ import javax.ws.rs.*; import javax.ws.rs.core.MediaType; -import java.util.UUID; @Component @Path("/accounts/login") @@ -17,10 +16,7 @@ /** * ログイン時のトークン認証 - * アカウントが存在して, かつトークンを持っているかを確認する. - * - * @param uid ユーザーID - * @param token トークン + * アカウントが存在して, かつトークンを持っているかを確認する */ @GET @Produces(MediaType.APPLICATION_JSON) @@ -29,36 +25,30 @@ var searchAccount = accounts.getAccountByUid(uid); if (searchAccount == null) throw new WebApplicationException(404); - if (token != searchAccount.getToken()) throw new WebApplicationException(400); + if (!searchAccount.getToken().equals(token)) throw new WebApplicationException(400); return true; } - // ToDo: トークン発行をJsonで返すのか, クラスで返却するのか決める. - // ToDo: クラスなら void でいい → setterを Account に用意 - //----------------------------------------------------------- /** - * ログイン時のトークン発行 + * ログイン時, トークンをアカウントに発行させる * * @param email ユーザーの登録したメアド * @param password パスワード */ @PUT @Produces(MediaType.APPLICATION_JSON) - public String getSignLoginToken(@FormParam("e-mail") String email, @FormParam("password") String password) { + public void authenticateLoginToken(@FormParam("e-mail") String email, @FormParam("password") String password) { var searchAccount = accounts.getAccountByEMail(email); if (searchAccount == null) throw new WebApplicationException(404); -// if(searchAccount.authPassword(password)) throw new WebApplicationException(401); + if (searchAccount.isMatchedPassword(password)) throw new WebApplicationException(401); - var newToken = UUID.randomUUID().toString(); - - - return newToken; + searchAccount.createToken(); } - + //----------------------------------------------------------- }