Skip to content

Commit

Permalink
Merge pull request #132 from integratedmodelling/IM-73-Users-dont-use…
Browse files Browse the repository at this point in the history
…-email-as-unique-and-primary-key-2

Im 73 users dont use email as unique and primary key 2
  • Loading branch information
kristinaBc3 authored Feb 16, 2024
2 parents 4ef1d69 + f810975 commit d7caa6e
Show file tree
Hide file tree
Showing 28 changed files with 667 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ public static interface HUB {
* Base URL path for node resources on the hub.
*/
public static final String NODE_BASE_ID = NODE_BASE + "/{id}";
/**
* Base URL path for user resources on the hub with no authentication.
*/
public static final String USER_BASE_NOAUTH = USER_BASE + "/noAuth";
/**
* Base URL path for user resources on the hub with no authentication.
*/
public static final String USER_BASE_ID_NOAUTH = USER_BASE_NOAUTH + "/{id}";
/**
* Base URL path for user resources on the hub.
*/
Expand Down Expand Up @@ -477,6 +485,18 @@ public static interface PARAMETERS {
* URL PARAMETER for user to verify account.
*/
public static final String USER_VERIFICATION = "verify";
/**
* URL PARAMETER for user get user profile by token.
*/
public static final String USER_GET = "get-user";
/**
* URL PARAMETER for user requesting a new email.
*/
public static final String USER_REQUEST_EMAIL = "request-new-email";
/**
* URL PARAMETER for user setting a new email from set email token.
*/
public static final String USER_SET_EMAIL = "new-email";
/**
* URL PARAMETER for user to request a new certificate.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class CustomProperty {
private String value;
private boolean onlyAdmin;

public CustomProperty() {}

public CustomProperty(String key, String value, boolean onlyAdmin) {
this.key = key;
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public enum ClickbackAction {
newUser(
TokenType.newUser),
lostPassword(
TokenType.lostPassword);
TokenType.lostPassword),
changeEmail(
TokenType.verifyEmail);

private final TokenType tokenType;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.integratedmodelling.klab.hub.api;

import org.integratedmodelling.klab.hub.commands.CreateTokenCommand;
import org.integratedmodelling.klab.hub.config.LinkConfig;
import org.integratedmodelling.klab.hub.repository.TokenRepository;

public class CreateVerifyEmailToken extends CreateTokenCommand {

private TokenRepository tokenRepository;
private String username;
private String email;

public CreateVerifyEmailToken(TokenRepository tokenRepository, String username,
LinkConfig linkConfig) {
this.tokenRepository = tokenRepository;
this.username = username;
setLinkConfig(linkConfig);
}

public CreateVerifyEmailToken(TokenRepository tokenRepository, String username, String email, LinkConfig linkConfig) {
this.tokenRepository = tokenRepository;
this.username = username;
this.email = email;
setLinkConfig(linkConfig);
}

@Override
public TokenVerifyEmailClickback execute() {

TokenVerifyEmailClickback token = new TokenVerifyEmailClickback(username, email);
token.setCallbackUrl(getLinkConfig());
token.setAuthenticated(true);
tokenRepository.save(token);
return token;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ProfileResource implements OAuth2User{

public String address;

public String jobTitle;
public String jobTitle;

public String phone;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public enum TokenType {
lostPassword(
TokenLostPasswordClickback.class),
lever(
TokenLever.class);
TokenLever.class),
verifyEmail(
TokenVerifyEmailClickback.class);

private final Class<? extends TokenAuthentication> clazz;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
package org.integratedmodelling.klab.hub.api;

import org.integratedmodelling.klab.hub.config.LinkConfig;
import org.springframework.data.annotation.TypeAlias;

@TypeAlias("VerifyEmail")
public class TokenVerifyEmailClickback extends TokenClickback {

private static final long serialVersionUID = 2577854654763037014L;
private String newEmail;



public TokenVerifyEmailClickback(String username) {
public TokenVerifyEmailClickback(String username, String newEmail) {
super(username);
this.newEmail = newEmail;
}

public void setNewEmailAddress(String newEmailAddress) {


@Override
public String getSuccessUrl(LinkConfig tokenClickbackConfig) {
return null;
}

@Override
public ClickbackAction getClickbackAction() {
return null;
return ClickbackAction.changeEmail;
}

public String getNewEmail() {
return newEmail;
}

public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.integratedmodelling.klab.hub.payload;

import org.integratedmodelling.klab.rest.UserAuthenticationRequest;

public class UpdateEmailRequest extends UserAuthenticationRequest{

public String email;
public String token;

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.integratedmodelling.klab.hub.payload;

public class UpdateEmailResponse {

private String email;
private String newEmail;


public UpdateEmailResponse() {
super();
}

public UpdateEmailResponse(String email, String newEmail) {
super();
this.email = email;
this.newEmail = newEmail;
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNewEmail() {
return newEmail;
}
public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,47 @@ public final class HubRequestMatchers {
API.HUB.AUTHENTICATE_USER,
};

private static final String[] users = new String[] {
private static final String[] usersPost = new String[] {
API.HUB.USER_BASE+".*" + API.HUB.PARAMETERS.USER_ACTIVATION + ".*",
API.HUB.USER_BASE+".*" + API.HUB.PARAMETERS.USER_LOST_PASSWORD + ".*",
API.HUB.USER_BASE+".*" + API.HUB.PARAMETERS.USER_SET_PASSWORD + ".*",
API.HUB.USER_BASE+".*" + API.HUB.PARAMETERS.USER_VERIFICATION + ".*",
API.HUB.USER_BASE+"$"
};

private static final String[] usersPut = new String[] {
API.HUB.USER_BASE+".*" + API.HUB.PARAMETERS.USER_SET_EMAIL + ".*",
API.HUB.USER_BASE+"$"
};

private static final String[] agreements = new String[] {
API.HUB.AGREEMENT_TEMPLATE_TYPE_LEVEL+"*"
};

private static final String[] usersGet = new String[] {
API.HUB.USER_BASE_NOAUTH+".*" + API.HUB.PARAMETERS.USER_GET + ".*",
API.HUB.USER_BASE+"$"
};

public static String[] getAuthentication() {
return authentication;
}

public static String[] getUsers() {
return users;
public static String[] getUsersPost() {
return usersPost;
}

public static String[] getAgreements() {
return agreements;
}

public static String[] getUsersGet() {
return usersGet;
}

public static String[] getUsersPut() {
return usersPut;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ protected void configure(HttpSecurity http) throws Exception {
.permitAll()
.antMatchers(HttpMethod.POST, HubRequestMatchers.getAuthentication())
.permitAll()
.regexMatchers(HttpMethod.POST, HubRequestMatchers.getUsers())
.regexMatchers(HttpMethod.POST, HubRequestMatchers.getUsersPost())
.permitAll()
.regexMatchers(HttpMethod.GET, HubRequestMatchers.getUsersGet())
.permitAll()
.regexMatchers(HttpMethod.PUT, HubRequestMatchers.getUsersPut())
.permitAll()
.anyRequest()
.authenticated()
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface LdapService {
abstract void createUser(UserDetails user);
abstract Name buildDn(String username );
abstract boolean userExists(String username, String email);
void updateUserEmailAddress(String username, String newEmailAddress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;

import org.integratedmodelling.klab.hub.exception.BadRequestException;
import org.integratedmodelling.klab.hub.service.LdapService;
Expand Down Expand Up @@ -122,4 +125,11 @@ public HashMap<String, String> mapFromAttributes(Attributes attributes) throws N
return userAttributes;
}
}

@Override
public void updateUserEmailAddress(String username, String newEmailAddress) {
Name dn = buildDn(username);
ModificationItem modificationItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", newEmailAddress));
ldapTemplate.modifyAttributes(dn, new ModificationItem[] { modificationItem });
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.integratedmodelling.klab.hub.tokens.services;

import org.integratedmodelling.klab.hub.api.TokenAuthentication;
import org.integratedmodelling.klab.hub.api.TokenClickback;
import org.integratedmodelling.klab.hub.api.TokenType;
import org.springframework.stereotype.Service;
Expand All @@ -9,5 +10,7 @@ public interface RegistrationTokenService extends TokenBaseService<TokenClickbac
public TokenClickback createChildToken(String username, String parentToken, TokenType verify);
public abstract boolean verifyToken(String username, String tokenString, TokenType verify);
public abstract boolean verifyTokens(String username, String tokenString, TokenType ...verify);
public TokenClickback createToken(String username, String email, TokenType type);
public TokenAuthentication getAndVerifyToken(String username, String id, TokenType type);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.integratedmodelling.klab.hub.tokens.services;

import org.integratedmodelling.klab.hub.api.CreateVerifyEmailToken;
import org.integratedmodelling.klab.hub.api.TokenAuthentication;
import org.integratedmodelling.klab.hub.api.TokenClickback;
import org.integratedmodelling.klab.hub.api.TokenType;
Expand Down Expand Up @@ -34,6 +35,17 @@ public TokenClickback createToken(String username, TokenType type) {
return new CreateChangePasswordToken(repository, username, linkConfig).execute();
} else if(type.equals(TokenType.lostPassword)) {
return new CreateLostPasswordToken(repository, username, linkConfig).execute();
} else if(type.equals(TokenType.verifyEmail)) {
return new CreateVerifyEmailToken(repository, username, linkConfig).execute();
} else {
return null;
}
}

@Override
public TokenClickback createToken(String username, String email, TokenType type) {
if(type.equals(TokenType.verifyEmail)) {
return new CreateVerifyEmailToken(repository, username, email, linkConfig).execute();
} else {
return null;
}
Expand All @@ -58,6 +70,15 @@ public boolean verifyToken(String username, String id, TokenType type) {
.isPresent();
}

@Override
public TokenAuthentication getAndVerifyToken(String username, String id, TokenType type) {
return repository.findByTokenString(id)
.filter(token -> token.getPrincipal().equals(username))
.map(TokenClickback.class::cast)
.filter(token -> token.getClickbackAction().getTokenType().equals(type)).orElseGet(null);

}

@Override
public boolean verifyTokens(String username, String id, TokenType ...types) {
for (TokenType type: types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public interface UserAuthTokenService extends TokenBaseService<TokenAuthentication> {
public TokenAuthentication getUserAuthenticationToken(String username, String password);

public LoginResponse getAuthResponse(String username, String password, boolean jwtToken);
public LoginResponse<?> getAuthResponse(String username, String password, boolean jwtToken);

public LogoutResponse getLogoutResponse(String token);

Expand Down
Loading

0 comments on commit d7caa6e

Please sign in to comment.