Instantly share code, notes, and snippets.
Last active
May 10, 2022 22:25
-
Save sjohnr/f423d6317814a25e2c641a57fc6fa5ec to your computer and use it in GitHub Desktop.
JpaOAuth2AuthorizationConsentService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2020-2021 the original author or 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 | |
* | |
* https://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. | |
*/ | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.IdClass; | |
/** | |
* @author Steve Riesenberg | |
*/ | |
@Entity | |
@IdClass(AuthorizationConsent.AuthorizationConsentId.class) | |
public class AuthorizationConsent { | |
@Id | |
private String registeredClientId; | |
@Id | |
private String principalName; | |
@Column(length = 1000) | |
private String authorities; | |
public String getRegisteredClientId() { | |
return registeredClientId; | |
} | |
public void setRegisteredClientId(String registeredClientId) { | |
this.registeredClientId = registeredClientId; | |
} | |
public String getPrincipalName() { | |
return principalName; | |
} | |
public void setPrincipalName(String principalName) { | |
this.principalName = principalName; | |
} | |
public String getAuthorities() { | |
return authorities; | |
} | |
public void setAuthorities(String authorities) { | |
this.authorities = authorities; | |
} | |
public static class AuthorizationConsentId implements Serializable { | |
private String registeredClientId; | |
private String principalName; | |
public String getRegisteredClientId() { | |
return registeredClientId; | |
} | |
public void setRegisteredClientId(String registeredClientId) { | |
this.registeredClientId = registeredClientId; | |
} | |
public String getPrincipalName() { | |
return principalName; | |
} | |
public void setPrincipalName(String principalName) { | |
this.principalName = principalName; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2020-2021 the original author or 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 | |
* | |
* https://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. | |
*/ | |
import java.util.Optional; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Repository; | |
/** | |
* @author Steve Riesenberg | |
*/ | |
@Repository | |
public interface AuthorizationConsentRepository extends JpaRepository<AuthorizationConsent, AuthorizationConsent.AuthorizationConsentId> { | |
Optional<AuthorizationConsent> findByRegisteredClientIdAndPrincipalName(String registeredClientId, String principalName); | |
void deleteByRegisteredClientIdAndPrincipalName(String registeredClientId, String principalName); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2020-2021 the original author or 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 | |
* | |
* https://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. | |
*/ | |
import java.util.HashSet; | |
import java.util.Set; | |
import org.springframework.dao.DataRetrievalFailureException; | |
import org.springframework.security.core.GrantedAuthority; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent; | |
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; | |
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; | |
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; | |
import org.springframework.stereotype.Component; | |
import org.springframework.util.Assert; | |
import org.springframework.util.StringUtils; | |
/** | |
* @author Steve Riesenberg | |
*/ | |
@Component | |
public class JpaOAuth2AuthorizationConsentService implements OAuth2AuthorizationConsentService { | |
private final AuthorizationConsentRepository authorizationConsentRepository; | |
private final RegisteredClientRepository registeredClientRepository; | |
public JpaOAuth2AuthorizationConsentService(AuthorizationConsentRepository authorizationConsentRepository, RegisteredClientRepository registeredClientRepository) { | |
Assert.notNull(authorizationConsentRepository, "authorizationConsentRepository cannot be null"); | |
Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null"); | |
this.authorizationConsentRepository = authorizationConsentRepository; | |
this.registeredClientRepository = registeredClientRepository; | |
} | |
@Override | |
public void save(OAuth2AuthorizationConsent authorizationConsent) { | |
Assert.notNull(authorizationConsent, "authorizationConsent cannot be null"); | |
this.authorizationConsentRepository.save(toEntity(authorizationConsent)); | |
} | |
@Override | |
public void remove(OAuth2AuthorizationConsent authorizationConsent) { | |
Assert.notNull(authorizationConsent, "authorizationConsent cannot be null"); | |
this.authorizationConsentRepository.deleteByRegisteredClientIdAndPrincipalName( | |
authorizationConsent.getRegisteredClientId(), authorizationConsent.getPrincipalName()); | |
} | |
@Override | |
public OAuth2AuthorizationConsent findById(String registeredClientId, String principalName) { | |
Assert.hasText(registeredClientId, "registeredClientId cannot be empty"); | |
Assert.hasText(principalName, "principalName cannot be empty"); | |
return this.authorizationConsentRepository.findByRegisteredClientIdAndPrincipalName( | |
registeredClientId, principalName).map(this::toObject).orElse(null); | |
} | |
private OAuth2AuthorizationConsent toObject(AuthorizationConsent authorizationConsent) { | |
String registeredClientId = authorizationConsent.getRegisteredClientId(); | |
RegisteredClient registeredClient = this.registeredClientRepository.findById(registeredClientId); | |
if (registeredClient == null) { | |
throw new DataRetrievalFailureException( | |
"The RegisteredClient with id '" + registeredClientId + "' was not found in the RegisteredClientRepository."); | |
} | |
OAuth2AuthorizationConsent.Builder builder = OAuth2AuthorizationConsent.withId( | |
registeredClientId, authorizationConsent.getPrincipalName()); | |
if (authorizationConsent.getAuthorities() != null) { | |
for (String authority : StringUtils.commaDelimitedListToSet(authorizationConsent.getAuthorities())) { | |
builder.authority(new SimpleGrantedAuthority(authority)); | |
} | |
} | |
return builder.build(); | |
} | |
private AuthorizationConsent toEntity(OAuth2AuthorizationConsent authorizationConsent) { | |
AuthorizationConsent entity = new AuthorizationConsent(); | |
entity.setRegisteredClientId(authorizationConsent.getRegisteredClientId()); | |
entity.setPrincipalName(authorizationConsent.getPrincipalName()); | |
Set<String> authorities = new HashSet<>(); | |
for (GrantedAuthority authority : authorizationConsent.getAuthorities()) { | |
authorities.add(authority.getAuthority()); | |
} | |
entity.setAuthorities(StringUtils.collectionToCommaDelimitedString(authorities)); | |
return entity; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment