2021-11-30 17:39:26 +01:00
|
|
|
package org.gcube.common.authorization.utils.secret;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.exception.AuthorizationException;
|
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.authorization.library.utils.Caller;
|
2021-12-07 13:50:39 +01:00
|
|
|
import org.gcube.common.authorization.utils.socialservice.SocialService;
|
|
|
|
import org.gcube.common.authorization.utils.user.User;
|
2021-11-30 17:39:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class GCubeSecret extends Secret {
|
|
|
|
|
2021-12-01 10:57:22 +01:00
|
|
|
public static final String GCUBE_TOKEN_REGEX = "^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}-[a-fA-F0-9]{8,9}){1}$";
|
2021-11-30 17:39:26 +01:00
|
|
|
|
2021-12-01 11:48:22 +01:00
|
|
|
protected AuthorizationEntry authorizationEntry;
|
|
|
|
|
2021-11-30 17:39:26 +01:00
|
|
|
@Override
|
|
|
|
protected void check(String token) throws AuthorizationException {
|
|
|
|
super.check(token);
|
2021-12-01 10:57:22 +01:00
|
|
|
if(!Pattern.matches(GCubeSecret.GCUBE_TOKEN_REGEX, token)) {
|
|
|
|
throw new AuthorizationException("The GUCBE token must comply with the regex " + GCUBE_TOKEN_REGEX);
|
2021-11-30 17:39:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public GCubeSecret(String token) {
|
|
|
|
super(20, token);
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:48:22 +01:00
|
|
|
protected AuthorizationEntry getAuthorizationEntry() throws Exception {
|
|
|
|
if(authorizationEntry==null) {
|
|
|
|
authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
}
|
|
|
|
return authorizationEntry;
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:16:03 +01:00
|
|
|
@Override
|
2021-12-01 11:48:22 +01:00
|
|
|
public void setToken() throws Exception {
|
2021-11-30 17:39:26 +01:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:16:03 +01:00
|
|
|
@Override
|
|
|
|
public void resetToken() throws Exception {
|
|
|
|
SecurityTokenProvider.instance.reset();
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:48:22 +01:00
|
|
|
@Override
|
|
|
|
public ClientInfo getClientInfo() throws Exception {
|
|
|
|
return getAuthorizationEntry().getClientInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Caller getCaller() throws Exception {
|
|
|
|
ClientInfo clientInfo = getClientInfo();
|
|
|
|
String qualifier = authorizationEntry.getQualifier();
|
|
|
|
Caller caller = new Caller(clientInfo, qualifier);
|
|
|
|
return caller;
|
2021-11-30 17:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getContext() throws Exception {
|
2021-12-01 11:48:22 +01:00
|
|
|
return getAuthorizationEntry().getContext();
|
2021-11-30 17:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHTTPAuthorizationHeaders() {
|
|
|
|
Map<String, String> authorizationHeaders = new HashMap<>();
|
|
|
|
authorizationHeaders.put(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, token);
|
|
|
|
return authorizationHeaders;
|
|
|
|
}
|
|
|
|
|
2021-12-06 17:43:56 +01:00
|
|
|
@Override
|
|
|
|
public boolean isExpired() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRefreshable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-07 13:50:39 +01:00
|
|
|
public User getUser() {
|
|
|
|
if(user==null) {
|
|
|
|
try {
|
|
|
|
user = SocialService.getSocialService().getUser(this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException();
|
|
|
|
}
|
2021-12-06 17:43:56 +01:00
|
|
|
}
|
2021-12-07 13:50:39 +01:00
|
|
|
return user;
|
2021-12-06 17:43:56 +01:00
|
|
|
}
|
2021-12-07 13:50:39 +01:00
|
|
|
|
2021-11-30 17:39:26 +01:00
|
|
|
}
|