38 changed files with 1562 additions and 571 deletions
@ -0,0 +1,257 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.config; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; |
|||
import org.springframework.security.crypto.keygen.StringKeyGenerator; |
|||
import org.springframework.security.oauth2.client.registration.ClientRegistration; |
|||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
|||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestResolver; |
|||
import org.springframework.security.oauth2.core.AuthorizationGrantType; |
|||
import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
|||
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames; |
|||
import org.springframework.security.oauth2.core.oidc.OidcScopes; |
|||
import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames; |
|||
import org.springframework.security.web.util.UrlUtils; |
|||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.util.Assert; |
|||
import org.springframework.util.CollectionUtils; |
|||
import org.springframework.util.StringUtils; |
|||
import org.springframework.web.util.UriComponents; |
|||
import org.springframework.web.util.UriComponentsBuilder; |
|||
import org.thingsboard.server.dao.oauth2.OAuth2Configuration; |
|||
import org.thingsboard.server.utils.WebUtils; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.security.MessageDigest; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.util.Base64; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class CustomOAuth2AuthorizationRequestResolver implements OAuth2AuthorizationRequestResolver { |
|||
public static final String DEFAULT_AUTHORIZATION_REQUEST_BASE_URI = "/oauth2/authorization"; |
|||
public static final String DEFAULT_LOGIN_PROCESSING_URI = "/login/oauth2/code/"; |
|||
private static final String REGISTRATION_ID_URI_VARIABLE_NAME = "registrationId"; |
|||
private static final char PATH_DELIMITER = '/'; |
|||
|
|||
private final AntPathRequestMatcher authorizationRequestMatcher = new AntPathRequestMatcher( |
|||
DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/{" + REGISTRATION_ID_URI_VARIABLE_NAME + "}"); |
|||
private final StringKeyGenerator stateGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder()); |
|||
private final StringKeyGenerator secureKeyGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); |
|||
|
|||
@Autowired |
|||
private ClientRegistrationRepository clientRegistrationRepository; |
|||
|
|||
@Autowired(required = false) |
|||
private OAuth2Configuration oauth2Configuration; |
|||
|
|||
|
|||
@Override |
|||
public OAuth2AuthorizationRequest resolve(HttpServletRequest request) { |
|||
String registrationId = this.resolveRegistrationId(request); |
|||
String redirectUriAction = getAction(request, "login"); |
|||
return resolve(request, registrationId, redirectUriAction); |
|||
} |
|||
|
|||
@Override |
|||
public OAuth2AuthorizationRequest resolve(HttpServletRequest request, String registrationId) { |
|||
if (registrationId == null) { |
|||
return null; |
|||
} |
|||
String redirectUriAction = getAction(request, "authorize"); |
|||
return resolve(request, registrationId, redirectUriAction); |
|||
} |
|||
|
|||
private String getAction(HttpServletRequest request, String defaultAction) { |
|||
String action = request.getParameter("action"); |
|||
if (action == null) { |
|||
return defaultAction; |
|||
} |
|||
return action; |
|||
} |
|||
|
|||
private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String registrationId, String redirectUriAction) { |
|||
if (registrationId == null) { |
|||
return null; |
|||
} |
|||
|
|||
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(registrationId); |
|||
if (clientRegistration == null) { |
|||
throw new IllegalArgumentException("Invalid Client Registration with Id: " + registrationId); |
|||
} |
|||
|
|||
Map<String, Object> attributes = new HashMap<>(); |
|||
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId()); |
|||
|
|||
OAuth2AuthorizationRequest.Builder builder; |
|||
if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(clientRegistration.getAuthorizationGrantType())) { |
|||
builder = OAuth2AuthorizationRequest.authorizationCode(); |
|||
Map<String, Object> additionalParameters = new HashMap<>(); |
|||
if (!CollectionUtils.isEmpty(clientRegistration.getScopes()) && |
|||
clientRegistration.getScopes().contains(OidcScopes.OPENID)) { |
|||
// Section 3.1.2.1 Authentication Request - https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
|
|||
// scope
|
|||
// REQUIRED. OpenID Connect requests MUST contain the "openid" scope value.
|
|||
addNonceParameters(attributes, additionalParameters); |
|||
} |
|||
if (ClientAuthenticationMethod.NONE.equals(clientRegistration.getClientAuthenticationMethod())) { |
|||
addPkceParameters(attributes, additionalParameters); |
|||
} |
|||
builder.additionalParameters(additionalParameters); |
|||
} else if (AuthorizationGrantType.IMPLICIT.equals(clientRegistration.getAuthorizationGrantType())) { |
|||
builder = OAuth2AuthorizationRequest.implicit(); |
|||
} else { |
|||
throw new IllegalArgumentException("Invalid Authorization Grant Type (" + |
|||
clientRegistration.getAuthorizationGrantType().getValue() + |
|||
") for Client Registration with Id: " + clientRegistration.getRegistrationId()); |
|||
} |
|||
|
|||
String redirectUriStr = expandRedirectUri(request, clientRegistration, redirectUriAction); |
|||
|
|||
return builder |
|||
.clientId(clientRegistration.getClientId()) |
|||
.authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri()) |
|||
.redirectUri(redirectUriStr) |
|||
.scopes(clientRegistration.getScopes()) |
|||
.state(this.stateGenerator.generateKey()) |
|||
.attributes(attributes) |
|||
.build(); |
|||
} |
|||
|
|||
private String resolveRegistrationId(HttpServletRequest request) { |
|||
if (this.authorizationRequestMatcher.matches(request)) { |
|||
return this.authorizationRequestMatcher |
|||
.matcher(request).getVariables().get(REGISTRATION_ID_URI_VARIABLE_NAME); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* Expands the {@link ClientRegistration#getRedirectUriTemplate()} with following provided variables:<br/> |
|||
* - baseUrl (e.g. https://localhost/app) <br/>
|
|||
* - baseScheme (e.g. https) <br/> |
|||
* - baseHost (e.g. localhost) <br/> |
|||
* - basePort (e.g. :8080) <br/> |
|||
* - basePath (e.g. /app) <br/> |
|||
* - registrationId (e.g. google) <br/> |
|||
* - action (e.g. login) <br/> |
|||
* <p/> |
|||
* Null variables are provided as empty strings. |
|||
* <p/> |
|||
* Default redirectUriTemplate is: {@link org.springframework.security.config.oauth2.client}.CommonOAuth2Provider#DEFAULT_REDIRECT_URL |
|||
* |
|||
* @return expanded URI |
|||
*/ |
|||
private String expandRedirectUri(HttpServletRequest request, ClientRegistration clientRegistration, String action) { |
|||
Map<String, String> uriVariables = new HashMap<>(); |
|||
uriVariables.put("registrationId", clientRegistration.getRegistrationId()); |
|||
|
|||
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) |
|||
.replacePath(request.getContextPath()) |
|||
.replaceQuery(null) |
|||
.fragment(null) |
|||
.build(); |
|||
String scheme = uriComponents.getScheme(); |
|||
uriVariables.put("baseScheme", scheme == null ? "" : scheme); |
|||
String host = uriComponents.getHost(); |
|||
uriVariables.put("baseHost", host == null ? "" : host); |
|||
// following logic is based on HierarchicalUriComponents#toUriString()
|
|||
int port = uriComponents.getPort(); |
|||
uriVariables.put("basePort", port == -1 ? "" : ":" + port); |
|||
String path = uriComponents.getPath(); |
|||
if (StringUtils.hasLength(path)) { |
|||
if (path.charAt(0) != PATH_DELIMITER) { |
|||
path = PATH_DELIMITER + path; |
|||
} |
|||
} |
|||
uriVariables.put("basePath", path == null ? "" : path); |
|||
uriVariables.put("baseUrl", uriComponents.toUriString()); |
|||
|
|||
uriVariables.put("action", action == null ? "" : action); |
|||
|
|||
return UriComponentsBuilder.fromUriString(getRedirectUri(request)) |
|||
.buildAndExpand(uriVariables) |
|||
.toUriString(); |
|||
} |
|||
|
|||
private String getRedirectUri(HttpServletRequest request) { |
|||
String loginProcessingUri = oauth2Configuration != null ? oauth2Configuration.getLoginProcessingUrl() : DEFAULT_LOGIN_PROCESSING_URI; |
|||
|
|||
String scheme = WebUtils.getScheme(request); |
|||
String host = WebUtils.getHost(request); |
|||
String port = WebUtils.getPort(request); |
|||
log.trace("Scheme - {}, host - {}, port - {}.", scheme, host, port); |
|||
String requestHost = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); |
|||
return requestHost + loginProcessingUri; |
|||
} |
|||
|
|||
/** |
|||
* Creates nonce and its hash for use in OpenID Connect 1.0 Authentication Requests. |
|||
* |
|||
* @param attributes where the {@link OidcParameterNames#NONCE} is stored for the authentication request |
|||
* @param additionalParameters where the {@link OidcParameterNames#NONCE} hash is added for the authentication request |
|||
* |
|||
* @since 5.2 |
|||
* @see <a target="_blank" href="https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest">3.1.2.1. Authentication Request</a> |
|||
*/ |
|||
private void addNonceParameters(Map<String, Object> attributes, Map<String, Object> additionalParameters) { |
|||
try { |
|||
String nonce = this.secureKeyGenerator.generateKey(); |
|||
String nonceHash = createHash(nonce); |
|||
attributes.put(OidcParameterNames.NONCE, nonce); |
|||
additionalParameters.put(OidcParameterNames.NONCE, nonceHash); |
|||
} catch (NoSuchAlgorithmException e) { } |
|||
} |
|||
|
|||
/** |
|||
* Creates and adds additional PKCE parameters for use in the OAuth 2.0 Authorization and Access Token Requests |
|||
* |
|||
* @param attributes where {@link PkceParameterNames#CODE_VERIFIER} is stored for the token request |
|||
* @param additionalParameters where {@link PkceParameterNames#CODE_CHALLENGE} and, usually, |
|||
* {@link PkceParameterNames#CODE_CHALLENGE_METHOD} are added to be used in the authorization request. |
|||
* |
|||
* @since 5.2 |
|||
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7636#section-1.1">1.1. Protocol Flow</a> |
|||
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7636#section-4.1">4.1. Client Creates a Code Verifier</a> |
|||
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7636#section-4.2">4.2. Client Creates the Code Challenge</a> |
|||
*/ |
|||
private void addPkceParameters(Map<String, Object> attributes, Map<String, Object> additionalParameters) { |
|||
String codeVerifier = this.secureKeyGenerator.generateKey(); |
|||
attributes.put(PkceParameterNames.CODE_VERIFIER, codeVerifier); |
|||
try { |
|||
String codeChallenge = createHash(codeVerifier); |
|||
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, codeChallenge); |
|||
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256"); |
|||
} catch (NoSuchAlgorithmException e) { |
|||
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, codeVerifier); |
|||
} |
|||
} |
|||
|
|||
private static String createHash(String value) throws NoSuchAlgorithmException { |
|||
MessageDigest md = MessageDigest.getInstance("SHA-256"); |
|||
byte[] digest = md.digest(value.getBytes(StandardCharsets.US_ASCII)); |
|||
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest); |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.utils; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.lang.StringUtils; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
|
|||
@Slf4j |
|||
public class WebUtils { |
|||
private static final String X_FORWARDED_HOST_HEADER_KEY = "x-forwarded-host"; |
|||
private static final String X_FORWARDED_PORT_HEADER_KEY = "x-forwarded-port"; |
|||
private static final String X_FORWARDED_PROTO_HEADER_KEY = "x-forwarded-proto"; |
|||
|
|||
public static String getHost(HttpServletRequest request) { |
|||
String forwardedHost = request.getHeader(X_FORWARDED_HOST_HEADER_KEY); |
|||
log.trace("Forwarded host - {}.", forwardedHost); |
|||
if (!StringUtils.isEmpty(forwardedHost)) { |
|||
if (forwardedHost.contains(":")) { |
|||
return forwardedHost.substring(0, forwardedHost.indexOf(":")); |
|||
} else { |
|||
return forwardedHost; |
|||
} |
|||
} else { |
|||
return request.getServerName(); |
|||
} |
|||
} |
|||
|
|||
public static String getScheme(HttpServletRequest request) { |
|||
String forwardedProto = request.getHeader(X_FORWARDED_PROTO_HEADER_KEY); |
|||
log.trace("Forwarded proto - {}.", forwardedProto); |
|||
if (!StringUtils.isEmpty(forwardedProto)) { |
|||
return forwardedProto; |
|||
} else { |
|||
return request.getServerName(); |
|||
} |
|||
} |
|||
|
|||
public static String getPort(HttpServletRequest request) { |
|||
String forwardedPort = request.getHeader(X_FORWARDED_PORT_HEADER_KEY); |
|||
log.trace("Forwarded port - {}.", forwardedPort); |
|||
if (!StringUtils.isEmpty(forwardedPort)) { |
|||
return forwardedPort; |
|||
} |
|||
String forwardedHost = request.getHeader(X_FORWARDED_HOST_HEADER_KEY); |
|||
if (!StringUtils.isEmpty(forwardedHost)) { |
|||
if (forwardedHost.contains(":")) { |
|||
return forwardedHost.substring(forwardedHost.indexOf(":")); |
|||
} else { |
|||
return "HTTP".equals(getScheme(request).toUpperCase()) ? |
|||
"80" : "443"; |
|||
} |
|||
} |
|||
return Integer.toString(request.getServerPort()); |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.id; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonCreator; |
|||
import com.fasterxml.jackson.annotation.JsonProperty; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public class OAuth2ClientRegistrationInfoId extends UUIDBased implements EntityId { |
|||
|
|||
@JsonCreator |
|||
public OAuth2ClientRegistrationInfoId(@JsonProperty("id") UUID id) { |
|||
super(id); |
|||
} |
|||
|
|||
public static OAuth2ClientRegistrationInfoId fromString(String clientRegistrationInfoId) { |
|||
return new OAuth2ClientRegistrationInfoId(UUID.fromString(clientRegistrationInfoId)); |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.OAUTH2_CLIENT_REGISTRATION_INFO; |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.oauth2; |
|||
|
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
|
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Data |
|||
public class ExtendedOAuth2ClientRegistrationInfo extends OAuth2ClientRegistrationInfo { |
|||
|
|||
private String domainName; |
|||
private SchemeType domainScheme; |
|||
|
|||
public ExtendedOAuth2ClientRegistrationInfo() { |
|||
super(); |
|||
} |
|||
|
|||
public ExtendedOAuth2ClientRegistrationInfo(OAuth2ClientRegistrationInfo oAuth2ClientRegistrationInfo, |
|||
SchemeType domainScheme, |
|||
String domainName) { |
|||
super(oAuth2ClientRegistrationInfo); |
|||
this.domainScheme = domainScheme; |
|||
this.domainName = domainName; |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.oauth2; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonProperty; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import lombok.NoArgsConstructor; |
|||
import lombok.ToString; |
|||
import org.thingsboard.server.common.data.HasName; |
|||
import org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo; |
|||
import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationInfoId; |
|||
|
|||
import java.util.List; |
|||
|
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Data |
|||
@ToString(exclude = {"clientSecret"}) |
|||
@NoArgsConstructor |
|||
public class OAuth2ClientRegistrationInfo extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationInfoId> implements HasName { |
|||
|
|||
private boolean enabled; |
|||
private OAuth2MapperConfig mapperConfig; |
|||
private String clientId; |
|||
private String clientSecret; |
|||
private String authorizationUri; |
|||
private String accessTokenUri; |
|||
private List<String> scope; |
|||
private String userInfoUri; |
|||
private String userNameAttributeName; |
|||
private String jwkSetUri; |
|||
private String clientAuthenticationMethod; |
|||
private String loginButtonLabel; |
|||
private String loginButtonIcon; |
|||
|
|||
public OAuth2ClientRegistrationInfo(OAuth2ClientRegistrationInfo clientRegistration) { |
|||
super(clientRegistration); |
|||
this.enabled = clientRegistration.enabled; |
|||
this.mapperConfig = clientRegistration.mapperConfig; |
|||
this.clientId = clientRegistration.clientId; |
|||
this.clientSecret = clientRegistration.clientSecret; |
|||
this.authorizationUri = clientRegistration.authorizationUri; |
|||
this.accessTokenUri = clientRegistration.accessTokenUri; |
|||
this.scope = clientRegistration.scope; |
|||
this.userInfoUri = clientRegistration.userInfoUri; |
|||
this.userNameAttributeName = clientRegistration.userNameAttributeName; |
|||
this.jwkSetUri = clientRegistration.jwkSetUri; |
|||
this.clientAuthenticationMethod = clientRegistration.clientAuthenticationMethod; |
|||
this.loginButtonLabel = clientRegistration.loginButtonLabel; |
|||
this.loginButtonIcon = clientRegistration.loginButtonIcon; |
|||
} |
|||
|
|||
@Override |
|||
@JsonProperty(access = JsonProperty.Access.READ_ONLY) |
|||
public String getName() { |
|||
return loginButtonLabel; |
|||
} |
|||
|
|||
@Override |
|||
public String getSearchText() { |
|||
return getName(); |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.oauth2; |
|||
|
|||
public enum SchemeType { |
|||
HTTP, HTTPS, MIXED; |
|||
} |
|||
@ -0,0 +1,231 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.model.sql; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.hibernate.annotations.Type; |
|||
import org.hibernate.annotations.TypeDef; |
|||
import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationInfoId; |
|||
import org.thingsboard.server.common.data.oauth2.*; |
|||
import org.thingsboard.server.dao.model.BaseSqlEntity; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
import org.thingsboard.server.dao.util.mapping.JsonStringType; |
|||
|
|||
import javax.persistence.*; |
|||
import java.util.Arrays; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
@TypeDef(name = "json", typeClass = JsonStringType.class) |
|||
@MappedSuperclass |
|||
public abstract class AbstractOAuth2ClientRegistrationInfoEntity<T extends OAuth2ClientRegistrationInfo> extends BaseSqlEntity<T> { |
|||
|
|||
@Column(name = ModelConstants.OAUTH2_ENABLED_PROPERTY) |
|||
private Boolean enabled; |
|||
@Column(name = ModelConstants.OAUTH2_CLIENT_ID_PROPERTY) |
|||
private String clientId; |
|||
@Column(name = ModelConstants.OAUTH2_CLIENT_SECRET_PROPERTY) |
|||
private String clientSecret; |
|||
@Column(name = ModelConstants.OAUTH2_AUTHORIZATION_URI_PROPERTY) |
|||
private String authorizationUri; |
|||
@Column(name = ModelConstants.OAUTH2_TOKEN_URI_PROPERTY) |
|||
private String tokenUri; |
|||
@Column(name = ModelConstants.OAUTH2_SCOPE_PROPERTY) |
|||
private String scope; |
|||
@Column(name = ModelConstants.OAUTH2_USER_INFO_URI_PROPERTY) |
|||
private String userInfoUri; |
|||
@Column(name = ModelConstants.OAUTH2_USER_NAME_ATTRIBUTE_NAME_PROPERTY) |
|||
private String userNameAttributeName; |
|||
@Column(name = ModelConstants.OAUTH2_JWK_SET_URI_PROPERTY) |
|||
private String jwkSetUri; |
|||
@Column(name = ModelConstants.OAUTH2_CLIENT_AUTHENTICATION_METHOD_PROPERTY) |
|||
private String clientAuthenticationMethod; |
|||
@Column(name = ModelConstants.OAUTH2_LOGIN_BUTTON_LABEL_PROPERTY) |
|||
private String loginButtonLabel; |
|||
@Column(name = ModelConstants.OAUTH2_LOGIN_BUTTON_ICON_PROPERTY) |
|||
private String loginButtonIcon; |
|||
@Column(name = ModelConstants.OAUTH2_ALLOW_USER_CREATION_PROPERTY) |
|||
private Boolean allowUserCreation; |
|||
@Column(name = ModelConstants.OAUTH2_ACTIVATE_USER_PROPERTY) |
|||
private Boolean activateUser; |
|||
@Enumerated(EnumType.STRING) |
|||
@Column(name = ModelConstants.OAUTH2_MAPPER_TYPE_PROPERTY) |
|||
private MapperType type; |
|||
@Column(name = ModelConstants.OAUTH2_EMAIL_ATTRIBUTE_KEY_PROPERTY) |
|||
private String emailAttributeKey; |
|||
@Column(name = ModelConstants.OAUTH2_FIRST_NAME_ATTRIBUTE_KEY_PROPERTY) |
|||
private String firstNameAttributeKey; |
|||
@Column(name = ModelConstants.OAUTH2_LAST_NAME_ATTRIBUTE_KEY_PROPERTY) |
|||
private String lastNameAttributeKey; |
|||
@Enumerated(EnumType.STRING) |
|||
@Column(name = ModelConstants.OAUTH2_TENANT_NAME_STRATEGY_PROPERTY) |
|||
private TenantNameStrategyType tenantNameStrategy; |
|||
@Column(name = ModelConstants.OAUTH2_TENANT_NAME_PATTERN_PROPERTY) |
|||
private String tenantNamePattern; |
|||
@Column(name = ModelConstants.OAUTH2_CUSTOMER_NAME_PATTERN_PROPERTY) |
|||
private String customerNamePattern; |
|||
@Column(name = ModelConstants.OAUTH2_DEFAULT_DASHBOARD_NAME_PROPERTY) |
|||
private String defaultDashboardName; |
|||
@Column(name = ModelConstants.OAUTH2_ALWAYS_FULL_SCREEN_PROPERTY) |
|||
private Boolean alwaysFullScreen; |
|||
@Column(name = ModelConstants.OAUTH2_MAPPER_URL_PROPERTY) |
|||
private String url; |
|||
@Column(name = ModelConstants.OAUTH2_MAPPER_USERNAME_PROPERTY) |
|||
private String username; |
|||
@Column(name = ModelConstants.OAUTH2_MAPPER_PASSWORD_PROPERTY) |
|||
private String password; |
|||
@Column(name = ModelConstants.OAUTH2_MAPPER_SEND_TOKEN_PROPERTY) |
|||
private Boolean sendToken; |
|||
|
|||
@Type(type = "json") |
|||
@Column(name = ModelConstants.OAUTH2_ADDITIONAL_INFO_PROPERTY) |
|||
private JsonNode additionalInfo; |
|||
|
|||
public AbstractOAuth2ClientRegistrationInfoEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public AbstractOAuth2ClientRegistrationInfoEntity(OAuth2ClientRegistrationInfo clientRegistrationInfo) { |
|||
if (clientRegistrationInfo.getId() != null) { |
|||
this.setUuid(clientRegistrationInfo.getId().getId()); |
|||
} |
|||
this.createdTime = clientRegistrationInfo.getCreatedTime(); |
|||
this.enabled = clientRegistrationInfo.isEnabled(); |
|||
this.clientId = clientRegistrationInfo.getClientId(); |
|||
this.clientSecret = clientRegistrationInfo.getClientSecret(); |
|||
this.authorizationUri = clientRegistrationInfo.getAuthorizationUri(); |
|||
this.tokenUri = clientRegistrationInfo.getAccessTokenUri(); |
|||
this.scope = clientRegistrationInfo.getScope().stream().reduce((result, element) -> result + "," + element).orElse(""); |
|||
this.userInfoUri = clientRegistrationInfo.getUserInfoUri(); |
|||
this.userNameAttributeName = clientRegistrationInfo.getUserNameAttributeName(); |
|||
this.jwkSetUri = clientRegistrationInfo.getJwkSetUri(); |
|||
this.clientAuthenticationMethod = clientRegistrationInfo.getClientAuthenticationMethod(); |
|||
this.loginButtonLabel = clientRegistrationInfo.getLoginButtonLabel(); |
|||
this.loginButtonIcon = clientRegistrationInfo.getLoginButtonIcon(); |
|||
this.additionalInfo = clientRegistrationInfo.getAdditionalInfo(); |
|||
OAuth2MapperConfig mapperConfig = clientRegistrationInfo.getMapperConfig(); |
|||
if (mapperConfig != null) { |
|||
this.allowUserCreation = mapperConfig.isAllowUserCreation(); |
|||
this.activateUser = mapperConfig.isActivateUser(); |
|||
this.type = mapperConfig.getType(); |
|||
OAuth2BasicMapperConfig basicConfig = mapperConfig.getBasic(); |
|||
if (basicConfig != null) { |
|||
this.emailAttributeKey = basicConfig.getEmailAttributeKey(); |
|||
this.firstNameAttributeKey = basicConfig.getFirstNameAttributeKey(); |
|||
this.lastNameAttributeKey = basicConfig.getLastNameAttributeKey(); |
|||
this.tenantNameStrategy = basicConfig.getTenantNameStrategy(); |
|||
this.tenantNamePattern = basicConfig.getTenantNamePattern(); |
|||
this.customerNamePattern = basicConfig.getCustomerNamePattern(); |
|||
this.defaultDashboardName = basicConfig.getDefaultDashboardName(); |
|||
this.alwaysFullScreen = basicConfig.isAlwaysFullScreen(); |
|||
} |
|||
OAuth2CustomMapperConfig customConfig = mapperConfig.getCustom(); |
|||
if (customConfig != null) { |
|||
this.url = customConfig.getUrl(); |
|||
this.username = customConfig.getUsername(); |
|||
this.password = customConfig.getPassword(); |
|||
this.sendToken = customConfig.isSendToken(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public AbstractOAuth2ClientRegistrationInfoEntity(OAuth2ClientRegistrationInfoEntity oAuth2ClientRegistrationInfoEntity) { |
|||
this.setId(oAuth2ClientRegistrationInfoEntity.getId()); |
|||
this.setCreatedTime(oAuth2ClientRegistrationInfoEntity.getCreatedTime()); |
|||
this.enabled = oAuth2ClientRegistrationInfoEntity.getEnabled(); |
|||
this.clientId = oAuth2ClientRegistrationInfoEntity.getClientId(); |
|||
this.clientSecret = oAuth2ClientRegistrationInfoEntity.getClientSecret(); |
|||
this.authorizationUri = oAuth2ClientRegistrationInfoEntity.getAuthorizationUri(); |
|||
this.tokenUri = oAuth2ClientRegistrationInfoEntity.getTokenUri(); |
|||
this.scope = oAuth2ClientRegistrationInfoEntity.getScope(); |
|||
this.userInfoUri = oAuth2ClientRegistrationInfoEntity.getUserInfoUri(); |
|||
this.userNameAttributeName = oAuth2ClientRegistrationInfoEntity.getUserNameAttributeName(); |
|||
this.jwkSetUri = oAuth2ClientRegistrationInfoEntity.getJwkSetUri(); |
|||
this.clientAuthenticationMethod = oAuth2ClientRegistrationInfoEntity.getClientAuthenticationMethod(); |
|||
this.loginButtonLabel = oAuth2ClientRegistrationInfoEntity.getLoginButtonLabel(); |
|||
this.loginButtonIcon = oAuth2ClientRegistrationInfoEntity.getLoginButtonIcon(); |
|||
this.additionalInfo = oAuth2ClientRegistrationInfoEntity.getAdditionalInfo(); |
|||
this.allowUserCreation = oAuth2ClientRegistrationInfoEntity.getAllowUserCreation(); |
|||
this.activateUser = oAuth2ClientRegistrationInfoEntity.getActivateUser(); |
|||
this.type = oAuth2ClientRegistrationInfoEntity.getType(); |
|||
this.emailAttributeKey = oAuth2ClientRegistrationInfoEntity.getEmailAttributeKey(); |
|||
this.firstNameAttributeKey = oAuth2ClientRegistrationInfoEntity.getFirstNameAttributeKey(); |
|||
this.lastNameAttributeKey = oAuth2ClientRegistrationInfoEntity.getLastNameAttributeKey(); |
|||
this.tenantNameStrategy = oAuth2ClientRegistrationInfoEntity.getTenantNameStrategy(); |
|||
this.tenantNamePattern = oAuth2ClientRegistrationInfoEntity.getTenantNamePattern(); |
|||
this.customerNamePattern = oAuth2ClientRegistrationInfoEntity.getCustomerNamePattern(); |
|||
this.defaultDashboardName = oAuth2ClientRegistrationInfoEntity.getDefaultDashboardName(); |
|||
this.alwaysFullScreen = oAuth2ClientRegistrationInfoEntity.getAlwaysFullScreen(); |
|||
this.url = oAuth2ClientRegistrationInfoEntity.getUrl(); |
|||
this.username = oAuth2ClientRegistrationInfoEntity.getUsername(); |
|||
this.password = oAuth2ClientRegistrationInfoEntity.getPassword(); |
|||
this.sendToken = oAuth2ClientRegistrationInfoEntity.getSendToken(); |
|||
} |
|||
|
|||
|
|||
protected OAuth2ClientRegistrationInfo toOAuth2ClientRegistrationInfo() { |
|||
OAuth2ClientRegistrationInfo clientRegistrationInfo = new OAuth2ClientRegistrationInfo(); |
|||
clientRegistrationInfo.setId(new OAuth2ClientRegistrationInfoId(id)); |
|||
clientRegistrationInfo.setEnabled(enabled); |
|||
clientRegistrationInfo.setCreatedTime(createdTime); |
|||
clientRegistrationInfo.setAdditionalInfo(additionalInfo); |
|||
clientRegistrationInfo.setMapperConfig( |
|||
OAuth2MapperConfig.builder() |
|||
.allowUserCreation(allowUserCreation) |
|||
.activateUser(activateUser) |
|||
.type(type) |
|||
.basic( |
|||
type == MapperType.BASIC ? |
|||
OAuth2BasicMapperConfig.builder() |
|||
.emailAttributeKey(emailAttributeKey) |
|||
.firstNameAttributeKey(firstNameAttributeKey) |
|||
.lastNameAttributeKey(lastNameAttributeKey) |
|||
.tenantNameStrategy(tenantNameStrategy) |
|||
.tenantNamePattern(tenantNamePattern) |
|||
.customerNamePattern(customerNamePattern) |
|||
.defaultDashboardName(defaultDashboardName) |
|||
.alwaysFullScreen(alwaysFullScreen) |
|||
.build() |
|||
: null |
|||
) |
|||
.custom( |
|||
type == MapperType.CUSTOM ? |
|||
OAuth2CustomMapperConfig.builder() |
|||
.url(url) |
|||
.username(username) |
|||
.password(password) |
|||
.sendToken(sendToken) |
|||
.build() |
|||
: null |
|||
) |
|||
.build() |
|||
); |
|||
clientRegistrationInfo.setClientId(clientId); |
|||
clientRegistrationInfo.setClientSecret(clientSecret); |
|||
clientRegistrationInfo.setAuthorizationUri(authorizationUri); |
|||
clientRegistrationInfo.setAccessTokenUri(tokenUri); |
|||
clientRegistrationInfo.setScope(Arrays.asList(scope.split(","))); |
|||
clientRegistrationInfo.setUserInfoUri(userInfoUri); |
|||
clientRegistrationInfo.setUserNameAttributeName(userNameAttributeName); |
|||
clientRegistrationInfo.setJwkSetUri(jwkSetUri); |
|||
clientRegistrationInfo.setClientAuthenticationMethod(clientAuthenticationMethod); |
|||
clientRegistrationInfo.setLoginButtonLabel(loginButtonLabel); |
|||
clientRegistrationInfo.setLoginButtonIcon(loginButtonIcon); |
|||
return clientRegistrationInfo; |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.model.sql; |
|||
|
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.thingsboard.server.common.data.oauth2.ExtendedOAuth2ClientRegistrationInfo; |
|||
import org.thingsboard.server.common.data.oauth2.SchemeType; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
public class ExtendedOAuth2ClientRegistrationInfoEntity extends AbstractOAuth2ClientRegistrationInfoEntity<ExtendedOAuth2ClientRegistrationInfo> { |
|||
|
|||
private String domainName; |
|||
private SchemeType domainScheme; |
|||
|
|||
public ExtendedOAuth2ClientRegistrationInfoEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public ExtendedOAuth2ClientRegistrationInfoEntity(OAuth2ClientRegistrationInfoEntity oAuth2ClientRegistrationInfoEntity, |
|||
String domainName, |
|||
SchemeType domainScheme) { |
|||
super(oAuth2ClientRegistrationInfoEntity); |
|||
this.domainName = domainName; |
|||
this.domainScheme = domainScheme; |
|||
} |
|||
|
|||
@Override |
|||
public ExtendedOAuth2ClientRegistrationInfo toData() { |
|||
return new ExtendedOAuth2ClientRegistrationInfo(super.toOAuth2ClientRegistrationInfo(), |
|||
domainScheme, |
|||
domainName); |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.model.sql; |
|||
|
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.hibernate.annotations.TypeDef; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationInfo; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
import org.thingsboard.server.dao.util.mapping.JsonStringType; |
|||
|
|||
import javax.persistence.Entity; |
|||
import javax.persistence.Table; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Entity |
|||
@TypeDef(name = "json", typeClass = JsonStringType.class) |
|||
@Table(name = ModelConstants.OAUTH2_CLIENT_REGISTRATION_INFO_COLUMN_FAMILY_NAME) |
|||
public class OAuth2ClientRegistrationInfoEntity extends AbstractOAuth2ClientRegistrationInfoEntity<OAuth2ClientRegistrationInfo> { |
|||
|
|||
public OAuth2ClientRegistrationInfoEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public OAuth2ClientRegistrationInfoEntity(OAuth2ClientRegistrationInfo clientRegistration) { |
|||
super(clientRegistration); |
|||
} |
|||
|
|||
public OAuth2ClientRegistrationInfoEntity(OAuth2ClientRegistrationInfoEntity oAuth2ClientRegistrationInfoEntity) { |
|||
super(oAuth2ClientRegistrationInfoEntity); |
|||
} |
|||
|
|||
@Override |
|||
public OAuth2ClientRegistrationInfo toData() { |
|||
return super.toOAuth2ClientRegistrationInfo(); |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.oauth2; |
|||
|
|||
import org.thingsboard.server.common.data.oauth2.ExtendedOAuth2ClientRegistrationInfo; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationInfo; |
|||
import org.thingsboard.server.common.data.oauth2.SchemeType; |
|||
import org.thingsboard.server.dao.Dao; |
|||
|
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
public interface OAuth2ClientRegistrationInfoDao extends Dao<OAuth2ClientRegistrationInfo> { |
|||
List<OAuth2ClientRegistrationInfo> findAll(); |
|||
|
|||
List<ExtendedOAuth2ClientRegistrationInfo> findAllExtended(); |
|||
|
|||
List<OAuth2ClientRegistrationInfo> findByDomainSchemesAndDomainName(List<SchemeType> domainSchemes, String domainName); |
|||
|
|||
void deleteAll(); |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.oauth2; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.oauth2.ExtendedOAuth2ClientRegistrationInfo; |
|||
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationInfo; |
|||
import org.thingsboard.server.common.data.oauth2.SchemeType; |
|||
import org.thingsboard.server.dao.DaoUtil; |
|||
import org.thingsboard.server.dao.model.sql.OAuth2ClientRegistrationInfoEntity; |
|||
import org.thingsboard.server.dao.oauth2.OAuth2ClientRegistrationInfoDao; |
|||
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
import java.util.stream.Collectors; |
|||
|
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class JpaOAuth2ClientRegistrationInfoDao extends JpaAbstractDao<OAuth2ClientRegistrationInfoEntity, OAuth2ClientRegistrationInfo> implements OAuth2ClientRegistrationInfoDao { |
|||
private final OAuth2ClientRegistrationInfoRepository repository; |
|||
|
|||
@Override |
|||
protected Class<OAuth2ClientRegistrationInfoEntity> getEntityClass() { |
|||
return OAuth2ClientRegistrationInfoEntity.class; |
|||
} |
|||
|
|||
@Override |
|||
protected CrudRepository<OAuth2ClientRegistrationInfoEntity, UUID> getCrudRepository() { |
|||
return repository; |
|||
} |
|||
|
|||
@Override |
|||
public List<OAuth2ClientRegistrationInfo> findAll() { |
|||
Iterable<OAuth2ClientRegistrationInfoEntity> entities = repository.findAll(); |
|||
List<OAuth2ClientRegistrationInfo> result = new ArrayList<>(); |
|||
entities.forEach(entity -> { |
|||
result.add(DaoUtil.getData(entity)); |
|||
}); |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public List<ExtendedOAuth2ClientRegistrationInfo> findAllExtended() { |
|||
return repository.findAllExtended().stream() |
|||
.map(DaoUtil::getData) |
|||
.collect(Collectors.toList()); |
|||
} |
|||
|
|||
@Override |
|||
public List<OAuth2ClientRegistrationInfo> findByDomainSchemesAndDomainName(List<SchemeType> domainSchemes, String domainName) { |
|||
List<OAuth2ClientRegistrationInfoEntity> entities = repository.findAllByDomainSchemesAndName(domainSchemes, domainName); |
|||
return entities.stream().map(DaoUtil::getData).collect(Collectors.toList()); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteAll() { |
|||
repository.deleteAll(); |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
/** |
|||
* Copyright © 2016-2020 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.oauth2; |
|||
|
|||
import org.springframework.data.jpa.repository.Query; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.springframework.data.repository.query.Param; |
|||
import org.thingsboard.server.common.data.oauth2.SchemeType; |
|||
import org.thingsboard.server.dao.model.sql.ExtendedOAuth2ClientRegistrationInfoEntity; |
|||
import org.thingsboard.server.dao.model.sql.OAuth2ClientRegistrationInfoEntity; |
|||
|
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
public interface OAuth2ClientRegistrationInfoRepository extends CrudRepository<OAuth2ClientRegistrationInfoEntity, UUID> { |
|||
@Query("SELECT new OAuth2ClientRegistrationInfoEntity(cr_info) " + |
|||
"FROM OAuth2ClientRegistrationInfoEntity cr_info " + |
|||
"LEFT JOIN OAuth2ClientRegistrationEntity cr on cr_info.id = cr.clientRegistrationInfoId " + |
|||
"WHERE cr.domainName = :domainName " + |
|||
"AND cr.domainScheme IN (:domainSchemes)") |
|||
List<OAuth2ClientRegistrationInfoEntity> findAllByDomainSchemesAndName(@Param("domainSchemes") List<SchemeType> domainSchemes, |
|||
@Param("domainName") String domainName); |
|||
|
|||
@Query("SELECT new org.thingsboard.server.dao.model.sql.ExtendedOAuth2ClientRegistrationInfoEntity(cr_info, cr.domainName, cr.domainScheme) " + |
|||
"FROM OAuth2ClientRegistrationInfoEntity cr_info " + |
|||
"LEFT JOIN OAuth2ClientRegistrationEntity cr on cr_info.id = cr.clientRegistrationInfoId ") |
|||
List<ExtendedOAuth2ClientRegistrationInfoEntity> findAllExtended(); |
|||
} |
|||
Loading…
Reference in new issue