Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scopes and authorities to the authorization code table #7079

Draft
wants to merge 46 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
3279481
Deactivated records should get 409 on GET requests
amontenegro Feb 27, 2024
b3660c2
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Feb 28, 2024
2cc66ab
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 1, 2024
f2dc713
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 4, 2024
b4f8223
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 7, 2024
b0026c3
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 12, 2024
53e7616
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 19, 2024
bf82372
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 25, 2024
23b6afb
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 4, 2024
182c67d
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 4, 2024
c52ef13
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 8, 2024
d4f779b
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 8, 2024
27b0033
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 17, 2024
585b896
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 18, 2024
9c9dfef
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro May 8, 2024
42ac636
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro May 28, 2024
3f0d771
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jun 10, 2024
e65ec79
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jun 20, 2024
e69191a
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jun 24, 2024
cff8029
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 3, 2024
ddf3e67
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 4, 2024
a091bb3
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 5, 2024
97024f0
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 15, 2024
3201e7e
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 18, 2024
c85de91
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 22, 2024
3132245
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 30, 2024
3672cfd
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 6, 2024
6ef71b1
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 12, 2024
3ccccec
Sequence should start at 100000
amontenegro Aug 12, 2024
50b85a9
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 12, 2024
3338fbb
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 13, 2024
ebd6d1c
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 16, 2024
13e0a6f
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 19, 2024
ca4085d
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 30, 2024
a4e5704
Lets store the scopes and authrities directly into the auth code table
amontenegro Aug 30, 2024
1dba8a8
Add authorization_uri to the authorization code table
amontenegro Aug 30, 2024
56d0161
Change name
amontenegro Aug 30, 2024
de445a1
Add a field to store the short auth code
amontenegro Aug 30, 2024
ec09931
Storing the spring security request id, still not sure why we need it…
amontenegro Aug 31, 2024
72d4221
Shorter id names
amontenegro Aug 31, 2024
bad7061
Adding auth code expiration time
amontenegro Aug 31, 2024
538e0ba
Add the authorization state attribute field
amontenegro Sep 2, 2024
b616230
We dont need to store the short auth code in a different field
amontenegro Sep 5, 2024
f19e778
Adding spring session redis
amontenegro Sep 13, 2024
afd8239
This should be rolled back
amontenegro Sep 19, 2024
50456ef
Session is being loaded!
amontenegro Sep 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion orcid-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,18 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version>
<version>3.7.1</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>


</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public interface EmailManagerReadOnly extends ManagerReadOnlyBase {

EmailEntity find(String email);

Email findPrimaryEmail(String orcid);
Email findPrimaryEmail(String orcid);

String findPrimaryEmailValueFromCache(String orcid);

EmailFrequencyOptions getEmailFrequencyOptions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.orcid.utils.OrcidStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;

/**
*
Expand Down Expand Up @@ -185,7 +186,17 @@ public Email findPrimaryEmail(String orcid) {
}
return jpaJaxbEmailAdapter.toEmail(emailDao.findPrimaryEmail(orcid));
}


@Override
@Cacheable("primary-email-value")
public String findPrimaryEmailValueFromCache(String orcid) {
if(PojoUtil.isEmpty(orcid)) {
return null;
}
EmailEntity entity = emailDao.findPrimaryEmail(orcid);
return entity.getEmail();
}

@Override
public boolean isUsersOnlyEmail(String orcid, String email) {
List<EmailEntity> emails = emailDao.findByOrcid(orcid, getLastModified(orcid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,19 @@ public class OrcidProfileUserDetails implements UserDetails {

private static final long serialVersionUID = 1L;

private String orcid;
private final String orcid;

private String primaryEmail;

private String password;
private final String password;

private Collection<OrcidWebRole> grantedAuthorities = new HashSet<>();

public OrcidProfileUserDetails() {
}

public OrcidProfileUserDetails(String orcid, String primaryEmail, String password) {
public OrcidProfileUserDetails(String orcid, String password) {
this.orcid = orcid;
this.primaryEmail = primaryEmail;
this.password = password;
}

public OrcidProfileUserDetails(String orcid, String primaryEmail, String password, Collection<OrcidWebRole> grantedAuthorities) {
public OrcidProfileUserDetails(String orcid, String password, Collection<OrcidWebRole> grantedAuthorities) {
this.orcid = orcid;
this.primaryEmail = primaryEmail;
this.password = password;
this.grantedAuthorities = grantedAuthorities;
}
Expand Down Expand Up @@ -122,18 +115,13 @@ public String getOrcid() {
return orcid;
}

public String getPrimaryEmail() {
return primaryEmail;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((grantedAuthorities == null) ? 0 : grantedAuthorities.hashCode());
result = prime * result + ((orcid == null) ? 0 : orcid.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((primaryEmail == null) ? 0 : primaryEmail.hashCode());
return result;
}

Expand Down Expand Up @@ -161,11 +149,6 @@ public boolean equals(Object obj) {
return false;
} else if (!password.equals(other.password))
return false;
if (primaryEmail == null) {
if (other.primaryEmail != null)
return false;
} else if (!primaryEmail.equals(other.primaryEmail))
return false;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public class OrcidUserDetailsServiceImpl implements OrcidUserDetailsService {

@Resource
private EmailDao emailDao;

@Resource(name = "emailManagerReadOnlyV3")
protected EmailManagerReadOnly emailManagerReadOnly;


@Resource
private OrcidSecurityManager securityMgr;

@Resource (name = "emailManagerReadOnlyV3")
private EmailManagerReadOnly emailManagerReadOnly;

@Value("${org.orcid.core.baseUri}")
private String baseUrl;

Expand Down Expand Up @@ -107,43 +107,19 @@ public OrcidProfileUserDetails loadUserByProfile(ProfileEntity profile) {
}

private OrcidProfileUserDetails createUserDetails(ProfileEntity profile) {
String primaryEmail = retrievePrimaryEmail(profile);

String primaryEmail = retrievePrimaryEmail(profile.getId());
OrcidProfileUserDetails userDetails = null;

if (profile.getOrcidType() != null) {
OrcidType orcidType = OrcidType.valueOf(profile.getOrcidType());
userDetails = new OrcidProfileUserDetails(profile.getId(), primaryEmail, profile.getEncryptedPassword(), buildAuthorities(orcidType, profile.getGroupType() != null ? MemberType.valueOf(profile.getGroupType()) : null));
userDetails = new OrcidProfileUserDetails(profile.getId(), profile.getEncryptedPassword(), buildAuthorities(orcidType, profile.getGroupType() != null ? MemberType.valueOf(profile.getGroupType()) : null));
} else {
userDetails = new OrcidProfileUserDetails(profile.getId(), primaryEmail, profile.getEncryptedPassword());
userDetails = new OrcidProfileUserDetails(profile.getId(), profile.getEncryptedPassword());
}

return userDetails;
}

private String retrievePrimaryEmail(ProfileEntity profile) {
String orcid = profile.getId();
try {
return emailDao.findPrimaryEmail(orcid).getEmail();
} catch (javax.persistence.NoResultException nre) {
String alternativePrimaryEmail = emailDao.findNewestVerifiedOrNewestEmail(profile.getId());
emailDao.updatePrimary(orcid, alternativePrimaryEmail);

String message = String.format("User with orcid %s have no primary email, so, we are setting the newest verified email, or, the newest email in case non is verified as the primary one", orcid);
LOGGER.error(message);

return alternativePrimaryEmail;
} catch (javax.persistence.NonUniqueResultException nure) {
String alternativePrimaryEmail = emailDao.findNewestPrimaryEmail(profile.getId());
emailDao.updatePrimary(orcid, alternativePrimaryEmail);

String message = String.format("User with orcid %s have more than one primary email, so, we are setting the latest modified primary as the primary one", orcid);
LOGGER.error(message);

return alternativePrimaryEmail;
}
}

private void checkStatuses(ProfileEntity profile) {
if (profile.getPrimaryRecord() != null) {
throw new DeprecatedProfileException("orcid.frontend.security.deprecated_with_primary", profile.getPrimaryRecord().getId(), profile.getId());
Expand Down Expand Up @@ -207,9 +183,25 @@ else if (orcidType.equals(OrcidType.GROUP)) {
}

private List<OrcidWebRole> rolesAsList(OrcidWebRole... roles) {
// Make a mutable list
List<OrcidWebRole> list = new ArrayList<OrcidWebRole>(Arrays.asList(roles));
return list;
return new ArrayList<OrcidWebRole>(Arrays.asList(roles));
}

@Deprecated(forRemoval = true)
private String retrievePrimaryEmail(String orcid) {
try {
return emailDao.findPrimaryEmail(orcid).getEmail();
} catch (javax.persistence.NoResultException nre) {
String alternativePrimaryEmail = emailDao.findNewestVerifiedOrNewestEmail(orcid);
emailDao.updatePrimary(orcid, alternativePrimaryEmail);
String message = String.format("User with orcid %s have no primary email, so, we are setting the newest verified email, or, the newest email in case non is verified as the primary one", orcid);
LOGGER.error(message);
return alternativePrimaryEmail;
} catch (javax.persistence.NonUniqueResultException nure) {
String alternativePrimaryEmail = emailDao.findNewestPrimaryEmail(orcid);
emailDao.updatePrimary(orcid, alternativePrimaryEmail);
String message = String.format("User with orcid %s have more than one primary email, so, we are setting the latest modified primary as the primary one", orcid);
LOGGER.error(message);
return alternativePrimaryEmail;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static public void clearSecurityContext() {
}

static public void setupSecurityContextForWebUser(String userId, String email) {
OrcidProfileUserDetails details = new OrcidProfileUserDetails(userId, email, "password");
OrcidProfileUserDetails details = new OrcidProfileUserDetails(userId, email);
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userId, "password");
auth.setDetails(details);
SecurityContextImpl securityContext = new SecurityContextImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
Expand Down Expand Up @@ -31,7 +35,7 @@ public class RedisClient {
private final String redisPassword;
private final int cacheExpiryInSecs;
private final int clientTimeoutInMillis;
private JedisPool pool;
public JedisPool pool;
private SetParams defaultSetParams;

@Resource
Expand Down Expand Up @@ -67,7 +71,7 @@ public RedisClient(String redisHost, int redisPort, String password, int cacheEx
@PostConstruct
private void init() {
try {
JedisClientConfig config = DefaultJedisClientConfig.builder().connectionTimeoutMillis(this.clientTimeoutInMillis).timeoutMillis(this.clientTimeoutInMillis)
JedisClientConfig config = DefaultJedisClientConfig.builder().connectionTimeoutMillis(this.clientTimeoutInMillis)
.socketTimeoutMillis(this.clientTimeoutInMillis).password(this.redisPassword).ssl(true).build();
pool = new JedisPool(new HostAndPort(this.redisHost, this.redisPort), config);
defaultSetParams = new SetParams().ex(this.cacheExpiryInSecs);
Expand Down Expand Up @@ -141,4 +145,33 @@ public boolean remove(String key) {
}
return true;
}

public static void main(String [] args) {
RedisClient client = new RedisClient("reg-qa-redis-001.reg-qa-redis.3zksuc.use2.cache.amazonaws.com", 6379, "aVerySimpleToken");
client.init();
System.out.println("Connected");
Jedis r = client.pool.getResource();

Set<String> keys = r.keys("spring:session:sessions:*");

for(String key : keys) {
System.out.println("----------------------------------------------");
System.out.println(key);
String keyType = r.type(key);
System.out.println(keyType);
if("hash".equals(keyType)) {
Map<String, String> myMap = r.hgetAll(key);
for(String tkey : myMap.keySet()) {
System.out.println(tkey + ": " + myMap.get(tkey));
}
}
if("string".equals(keyType)) {
System.out.println(key + ": " + r.get("key"));
}
System.out.println("----------------------------------------------");
}



}
}
7 changes: 7 additions & 0 deletions orcid-core/src/main/resources/ehcache_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@
<ehcache:disk unit="MB" persistent="false">16</ehcache:disk>
</ehcache:resources>
</ehcache:cache>

<ehcache:cache alias="primary-email-value" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
<ehcache:tti unit="minutes">10</ehcache:tti>
</ehcache:expiry>
</ehcache:cache>

<ehcache:cache alias="count-tokens" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
Expand Down
7 changes: 7 additions & 0 deletions orcid-core/src/main/resources/ehcache_orcid-api-web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@
<ehcache:cache alias="emails" uses-template="bioTemplate" />

<ehcache:cache alias="public-emails" uses-template="bioTemplate" />

<ehcache:cache alias="primary-email-value" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
<ehcache:tti unit="minutes">10</ehcache:tti>
</ehcache:expiry>
</ehcache:cache>

<ehcache:cache alias="client-details" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
Expand Down
7 changes: 7 additions & 0 deletions orcid-core/src/main/resources/ehcache_orcid-internal-api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@
<ehcache:disk unit="MB" persistent="false">64</ehcache:disk>
</ehcache:resources>
</ehcache:cache>

<ehcache:cache alias="primary-email-value" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
<ehcache:tti unit="minutes">10</ehcache:tti>
</ehcache:expiry>
</ehcache:cache>

<ehcache:cache alias="count-tokens" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
Expand Down
9 changes: 8 additions & 1 deletion orcid-core/src/main/resources/ehcache_orcid-pub-web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@
<ehcache:cache alias="emails" uses-template="bioTemplate" />

<ehcache:cache alias="public-emails" uses-template="bioTemplate" />


<ehcache:cache alias="primary-email-value" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
<ehcache:tti unit="minutes">10</ehcache:tti>
</ehcache:expiry>
</ehcache:cache>

<ehcache:cache alias="client-details" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@
<ehcache:cache alias="emails" uses-template="bioTemplate" />

<ehcache:cache alias="public-emails" uses-template="bioTemplate" />


<ehcache:cache alias="primary-email-value" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
<ehcache:tti unit="minutes">10</ehcache:tti>
</ehcache:expiry>
</ehcache:cache>

<ehcache:cache alias="count-tokens" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:resources>
Expand Down
9 changes: 8 additions & 1 deletion orcid-core/src/main/resources/ehcache_orcid-web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@
<ehcache:cache alias="emails" uses-template="bioTemplate" />

<ehcache:cache alias="public-emails" uses-template="bioTemplate" />


<ehcache:cache alias="primary-email-value" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
<ehcache:tti unit="minutes">10</ehcache:tti>
</ehcache:expiry>
</ehcache:cache>

<ehcache:cache alias="client-details" uses-template="defaultTemplate">
<ehcache:value-type copier="org.ehcache.impl.copy.IdentityCopier"/>
<ehcache:expiry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public void loadUserByProfile_MoreThanOnePrimaryAvailable() {

assertNotNull(opud);
opud.getUsername();
assertEquals(email, opud.getPrimaryEmail());
assertEquals(ORCID, opud.getUsername());
}

Expand All @@ -233,7 +232,6 @@ public void loadUserByProfile_NoPrimaryAvailable() {

assertNotNull(opud);
opud.getUsername();
assertEquals(email, opud.getPrimaryEmail());
assertEquals(ORCID, opud.getUsername());
}

Expand Down
1 change: 1 addition & 0 deletions orcid-persistence/src/main/resources/db-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,5 @@
<include file="/db/updates/create_profile_email_domain_table.xml" />
<include file="/db/updates/create_dw_notification.xml" />
<include file="/db/updates/create_dw_profile_email_domain.xml" />
<include file="/db/updates/oauth2_authoriziation_code_detail_improvements.xml" />
</databaseChangeLog>
Loading
Loading