External auth providers, user display name

This commit is contained in:
serge-rider
2020-05-05 00:43:17 +03:00
parent 8dbb6d3029
commit 08f531afe8
5 changed files with 46 additions and 11 deletions
@@ -17,7 +17,10 @@
package io.cloudbeaver;
import io.cloudbeaver.model.user.WebUser;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.utils.CommonUtils;
import java.util.Map;
@@ -29,11 +32,16 @@ public interface DBWAuthProviderExternal<AUTH_SESSION> extends DBWAuthProvider<A
/**
* Returns new identifying credentials which can be used to find/create user in database
*/
@NotNull
Map<String, Object> readExternalCredentials(
Map<String, Object> providerConfig, // Auth provider configuration (e.g. 3rd party auth server address)
Map<String, Object> authParameters // Passed auth parameters (e.g. user name or password)
) throws DBException;
@NotNull
WebUser registerNewUser(DBWSecurityController securityController, Map<String, Object> providerConfig, Map<String, Object> credentials) throws DBException;
@Nullable
String getUserDisplayName(Map<String, Object> providerConfig, Map<String, Object> credentials);
}
@@ -30,6 +30,7 @@ public class WebUser {
@NotNull
private final String userId;
private String displayName;
private Map<String, Object> metaParameters = new LinkedHashMap<>();
private Map<String, Object> configurationParameters = new LinkedHashMap<>();
@@ -48,6 +49,17 @@ public class WebUser {
return userId;
}
/**
* User display name may be set by 3rd party auth providers
*/
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String[] getGrantedRoles() {
return roles.stream().map(WebRole::getId).toArray(String[]::new);
}
@@ -30,8 +30,13 @@ type AuthProviderInfo {
}
type UserAuthInfo {
# User unique identifier
userId: String!
# Human readable display name. May be null
displayName: String
# Auth provider ID
authProvider: String!
loginTime: DateTime!
# Optional login message
@@ -16,6 +16,8 @@
*/
package io.cloudbeaver.service.auth;
import io.cloudbeaver.model.user.WebUser;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.Date;
@@ -25,18 +27,22 @@ import java.util.Date;
*/
public class WebAuthInfo {
private String userId;
private WebUser user;
private String authProvider;
private Object authToken;
private OffsetDateTime loginTime;
private String message;
public String getUserId() {
return userId;
public WebAuthInfo(WebUser user) {
this.user = user;
}
public void setUserId(String userId) {
this.userId = userId;
public String getUserId() {
return user.getUserId();
}
public String getDisplayName() {
return user.getDisplayName();
}
public String getAuthProvider() {
@@ -84,18 +84,22 @@ public class WebServiceAuthImpl implements DBWServiceAuth {
providerConfig,
userCredentials,
authParameters);
WebAuthInfo authInfo = new WebAuthInfo();
authInfo.setUserId(userId);
authInfo.setLoginTime(OffsetDateTime.now());
authInfo.setAuthProvider(authProvider.getId());
authInfo.setAuthToken(authToken);
authInfo.setMessage("Authenticated with " + authProvider.getLabel() + " provider");
if (user == null) {
user = new WebUser(userId);
}
if (authProviderExternal != null) {
user.setDisplayName(authProviderExternal.getUserDisplayName(providerConfig, authParameters));
}
webSession.setUser(user);
WebAuthInfo authInfo = new WebAuthInfo(user);
authInfo.setLoginTime(OffsetDateTime.now());
authInfo.setAuthProvider(authProvider.getId());
authInfo.setAuthToken(authToken);
authInfo.setMessage("Authenticated with " + authProvider.getLabel() + " provider");
return authInfo;
} catch (DBException e) {
throw new DBWebException("User authentication failed", e);