-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature parity auth/portalUser reached (#2)
- can create user - can get user - can update user - login slightly changed
- Loading branch information
1 parent
e7b6ec2
commit 82f232c
Showing
11 changed files
with
299 additions
and
87 deletions.
There are no files selected for viewing
133 changes: 109 additions & 24 deletions
133
cake-catalog-auth-client/src/main/java/com/cakecatalog/srv/auth/client/AuthClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,137 @@ | ||
package com.cakecatalog.srv.auth.client; | ||
|
||
import com.cakecatalog.srv.auth.client.model.CreatePortalUserRequest; | ||
import com.cakecatalog.srv.auth.client.model.LoginRequest; | ||
import com.cakecatalog.srv.auth.client.model.PortalUserResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
|
||
public class AuthClient { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AuthClient.class); | ||
|
||
private String url = "http://localhost:8008/"; | ||
public static final String LOGIN_URL = "/login"; | ||
public static final String PORTAL_USERS_URL = "/portalUsers"; | ||
|
||
private String url; | ||
private RestTemplate restTemplate; | ||
|
||
AuthClient() {} | ||
AuthClient() { | ||
this("http://localhost:8008/"); | ||
} | ||
|
||
/** | ||
* Pass the base URL to the AuthService | ||
* @param url | ||
*/ | ||
AuthClient(String url) { | ||
this.url = url; | ||
restTemplate = new RestTemplate(); | ||
} | ||
|
||
//TODO need a client test | ||
public boolean login(LoginRequest loginRequest) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
Boolean isLoginSuccessful = restTemplate.postForObject( | ||
url + "/login", | ||
/** | ||
* Checks user credentials | ||
* | ||
* @param loginRequest - email and pass | ||
* @return logged user object or null if not succesful | ||
*/ | ||
public PortalUserResponse login(LoginRequest loginRequest) { | ||
log.info("Trying to log for user: " + loginRequest.getEmail()); | ||
PortalUserResponse loggedUser = restTemplate.postForObject( | ||
url + LOGIN_URL, | ||
loginRequest, | ||
Boolean.class); | ||
return isLoginSuccessful; | ||
PortalUserResponse.class); | ||
return loggedUser; | ||
} | ||
|
||
/** | ||
* Create a new user | ||
* @param name | ||
* @param email | ||
* @param password | ||
* @return result object | ||
*/ | ||
public PortalUserResponse createPortalUser(String name, String email, String password) { | ||
log.info("Trying to create user: " + email); | ||
|
||
PortalUserResponse createdUser = restTemplate.postForObject( | ||
url + PORTAL_USERS_URL, | ||
new CreatePortalUserRequest(name, email, password), | ||
PortalUserResponse.class); | ||
|
||
createdUser.setPassword(null);//hide password | ||
|
||
return createdUser; | ||
} | ||
|
||
/** | ||
* Update an existing user | ||
* @param userId | ||
* @param name | ||
* @param password | ||
*/ | ||
public void updatePortalUser(Long userId, String name, String password) { | ||
log.info("Trying to updating existing user with id: " + userId); | ||
|
||
PortalUserResponse portalUser = getPortalUser(userId); | ||
portalUser.setName(name); | ||
portalUser.setPassword(password); | ||
|
||
//this updates the whole object, thus we get the whole user object first | ||
restTemplate.put( | ||
url + PORTAL_USERS_URL + "/" + userId, | ||
portalUser); | ||
} | ||
|
||
/** | ||
* Get a user object by its ID | ||
* @param userId | ||
* @return | ||
*/ | ||
public PortalUserResponse getPortalUser(Long userId) { | ||
log.info("Trying to get user with id: " + userId); | ||
|
||
PortalUserResponse user = restTemplate.getForObject( | ||
URI.create(url + PORTAL_USERS_URL + "/" + userId), | ||
PortalUserResponse.class); | ||
|
||
user.setPassword(null);//hide password | ||
|
||
return user; | ||
} | ||
|
||
//TODO need a client test | ||
public static void main(String args[]) { | ||
boolean isSuccess = new AuthClient().login(new LoginRequest("[email protected]", "samplePassword")); | ||
log.info("Login is successful?: " + isSuccess); | ||
testUpdate(); | ||
testCreate(); | ||
testLogin(); | ||
} | ||
|
||
static public class LoginRequest { | ||
public String email; | ||
public String password; | ||
private static void testUpdate() { | ||
log.info("Create + Updating + getting"); | ||
AuthClient authClient = new AuthClient(); | ||
|
||
PortalUserResponse createdUser = authClient.createPortalUser("name", "email", "pass"); | ||
|
||
authClient.updatePortalUser(createdUser.getId(), "email-100", "pass-100"); | ||
|
||
public LoginRequest() { | ||
} // JAXB needs this | ||
assert authClient.getPortalUser(createdUser.getId()).getEmail() == "email-100"; | ||
} | ||
|
||
public LoginRequest(String name, String age) { | ||
this.email = name; | ||
this.password = age; | ||
} | ||
private static void testCreate() { | ||
PortalUserResponse createdPortalUser = new AuthClient().createPortalUser("name", "email", "pass"); | ||
log.info("Creation was successful?: " + createdPortalUser); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Email: " + email + "; Password: " + password; | ||
} | ||
private static void testLogin() { | ||
PortalUserResponse user = new AuthClient().login(new LoginRequest("[email protected]", "samplePassword")); | ||
log.info("Login is successful?: " + (user != null)); | ||
|
||
PortalUserResponse user2 = new AuthClient().login(new LoginRequest("[email protected]", "wrong-pass")); | ||
log.info("Login is successful?: " + (user2 != null)); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...h-client/src/main/java/com/cakecatalog/srv/auth/client/model/CreatePortalUserRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.cakecatalog.srv.auth.client.model; | ||
|
||
public class CreatePortalUserRequest { | ||
private String name; | ||
private String email; | ||
private String password; | ||
|
||
public CreatePortalUserRequest(String name, String email, String password) { | ||
this.setName(name); | ||
this.setEmail(email); | ||
this.setPassword(password); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...catalog-auth-client/src/main/java/com/cakecatalog/srv/auth/client/model/LoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.cakecatalog.srv.auth.client.model; | ||
|
||
public class LoginRequest { | ||
private String email; | ||
private String password; | ||
|
||
public LoginRequest() { | ||
} // JAXB needs this | ||
|
||
public LoginRequest(String name, String age) { | ||
this.setEmail(name); | ||
this.setPassword(age); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Email: " + getEmail() + "; Password: " + getPassword(); | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...g-auth-client/src/main/java/com/cakecatalog/srv/auth/client/model/PortalUserResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.cakecatalog.srv.auth.client.model; | ||
|
||
public class PortalUserResponse { | ||
|
||
private Long id; | ||
private String name; | ||
private String email; | ||
private String password; | ||
|
||
@Override | ||
public String toString() { | ||
return "PortalUserResponse{" + | ||
"id=" + getId() + | ||
", name='" + getName() + '\'' + | ||
", email='" + getEmail() + '\'' + | ||
'}'; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
cake-catalog-auth-server/src/main/java/com/cakecatalog/srv/auth/RepositoryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.cakecatalog.srv.auth; | ||
|
||
import com.cakecatalog.srv.auth.domain.PortalUser; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; | ||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; | ||
|
||
@Configuration | ||
public class RepositoryConfig extends RepositoryRestConfigurerAdapter { | ||
@Override | ||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { | ||
config.exposeIdsFor(PortalUser.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.