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.AuthorizationProvider;
|
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.authorization.library.utils.Caller;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2021-11-30 18:04:52 +01:00
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
2021-11-30 17:39:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class GCubeSecret extends Secret {
|
|
|
|
|
|
|
|
public static final String 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}$";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void check(String token) throws AuthorizationException {
|
|
|
|
super.check(token);
|
|
|
|
if(!Pattern.matches(GCubeSecret.TOKEN_REGEX, token)) {
|
|
|
|
throw new AuthorizationException("The GUCBE token must comply with the regex " + TOKEN_REGEX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public GCubeSecret(String token) {
|
|
|
|
super(20, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void set() throws Exception {
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
2021-11-30 18:04:52 +01:00
|
|
|
|
2021-11-30 17:39:26 +01:00
|
|
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
|
|
|
String qualifier = authorizationEntry.getQualifier();
|
|
|
|
Caller caller = new Caller(clientInfo, qualifier);
|
|
|
|
AuthorizationProvider.instance.set(caller);
|
2021-11-30 18:04:52 +01:00
|
|
|
|
|
|
|
ScopeBean scopeBean = new ScopeBean(getContext());
|
|
|
|
ScopeProvider.instance.set(scopeBean.toString());
|
2021-11-30 17:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected ClientInfo getClientInfo() throws Exception {
|
|
|
|
return Constants.authorizationService().get(token).getClientInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getContext() throws Exception {
|
|
|
|
return Constants.authorizationService().get(token).getContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUsername() throws Exception {
|
|
|
|
return getClientInfo().getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|