2020-06-04 03:08:52 +07:00
|
|
|
/*
|
2021-01-18 21:31:06 +07:00
|
|
|
* Copyright 2020-2021 the original author or authors.
|
2020-06-04 03:08:52 +07:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.oauth2.server.authorization;
|
|
|
|
|
2021-02-09 09:14:33 +07:00
|
|
|
import java.security.Principal;
|
2021-01-18 21:31:06 +07:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
2021-02-09 02:57:15 +07:00
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
2020-10-14 14:06:23 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
2020-11-10 01:38:33 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
2020-10-23 01:03:24 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.token.OAuth2AuthorizationCode;
|
2020-06-04 03:08:52 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Joe Grandja
|
2020-10-01 02:20:38 +07:00
|
|
|
* @author Daniel Garnier-Moiroux
|
2020-06-04 03:08:52 +07:00
|
|
|
*/
|
|
|
|
public class TestOAuth2Authorizations {
|
|
|
|
|
|
|
|
public static OAuth2Authorization.Builder authorization() {
|
2020-06-12 00:25:18 +07:00
|
|
|
return authorization(TestRegisteredClients.registeredClient().build());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static OAuth2Authorization.Builder authorization(RegisteredClient registeredClient) {
|
2020-06-23 02:35:01 +07:00
|
|
|
return authorization(registeredClient, Collections.emptyMap());
|
|
|
|
}
|
|
|
|
|
2020-10-01 02:20:38 +07:00
|
|
|
public static OAuth2Authorization.Builder authorization(RegisteredClient registeredClient,
|
|
|
|
Map<String, Object> authorizationRequestAdditionalParameters) {
|
2020-10-23 01:03:24 +07:00
|
|
|
OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode(
|
|
|
|
"code", Instant.now(), Instant.now().plusSeconds(120));
|
2020-06-04 03:08:52 +07:00
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(
|
|
|
|
OAuth2AccessToken.TokenType.BEARER, "access-token", Instant.now(), Instant.now().plusSeconds(300));
|
2020-11-10 01:38:33 +07:00
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2(
|
2020-10-14 14:06:23 +07:00
|
|
|
"refresh-token", Instant.now(), Instant.now().plus(1, ChronoUnit.HOURS));
|
2020-06-04 03:08:52 +07:00
|
|
|
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
|
|
|
|
.authorizationUri("https://provider.com/oauth2/authorize")
|
|
|
|
.clientId(registeredClient.getClientId())
|
2020-06-12 00:25:18 +07:00
|
|
|
.redirectUri(registeredClient.getRedirectUris().iterator().next())
|
2020-09-22 22:57:50 +07:00
|
|
|
.scopes(registeredClient.getScopes())
|
2020-10-01 02:20:38 +07:00
|
|
|
.additionalParameters(authorizationRequestAdditionalParameters)
|
2020-06-04 03:08:52 +07:00
|
|
|
.state("state")
|
|
|
|
.build();
|
|
|
|
return OAuth2Authorization.withRegisteredClient(registeredClient)
|
|
|
|
.principalName("principal")
|
2021-02-09 02:57:15 +07:00
|
|
|
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
2021-02-06 01:20:17 +07:00
|
|
|
.token(authorizationCode)
|
|
|
|
.accessToken(accessToken)
|
|
|
|
.refreshToken(refreshToken)
|
2021-02-09 15:25:21 +07:00
|
|
|
.attribute(OAuth2AuthorizationRequest.class.getName(), authorizationRequest)
|
2021-02-09 09:14:33 +07:00
|
|
|
.attribute(Principal.class.getName(),
|
2021-01-18 21:31:06 +07:00
|
|
|
new TestingAuthenticationToken("principal", null, "ROLE_A", "ROLE_B"))
|
2021-02-09 15:25:21 +07:00
|
|
|
.attribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizationRequest.getScopes());
|
2020-06-04 03:08:52 +07:00
|
|
|
}
|
|
|
|
}
|