2020-04-24 03:45:34 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.oauth2.server.authorization.authentication;
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable;
|
|
|
|
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
|
|
|
import org.springframework.security.core.Authentication;
|
2020-07-22 16:06:09 +07:00
|
|
|
import org.springframework.security.core.SpringSecurityCoreVersion2;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.util.Assert;
|
2020-04-24 03:45:34 +07:00
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
/**
|
2020-06-04 03:08:52 +07:00
|
|
|
* An {@link Authentication} implementation used for the OAuth 2.0 Authorization Code Grant.
|
|
|
|
*
|
2020-04-24 03:45:34 +07:00
|
|
|
* @author Joe Grandja
|
2020-05-07 23:24:58 +07:00
|
|
|
* @author Madhu Bhat
|
2020-06-04 03:08:52 +07:00
|
|
|
* @since 0.0.1
|
|
|
|
* @see AbstractAuthenticationToken
|
|
|
|
* @see OAuth2AuthorizationCodeAuthenticationProvider
|
|
|
|
* @see OAuth2ClientAuthenticationToken
|
2020-04-24 03:45:34 +07:00
|
|
|
*/
|
|
|
|
public class OAuth2AuthorizationCodeAuthenticationToken extends AbstractAuthenticationToken {
|
2020-07-22 16:06:09 +07:00
|
|
|
private static final long serialVersionUID = SpringSecurityCoreVersion2.SERIAL_VERSION_UID;
|
2020-04-24 03:45:34 +07:00
|
|
|
private String code;
|
|
|
|
private Authentication clientPrincipal;
|
|
|
|
private String clientId;
|
|
|
|
private String redirectUri;
|
|
|
|
|
2020-06-04 03:08:52 +07:00
|
|
|
/**
|
|
|
|
* Constructs an {@code OAuth2AuthorizationCodeAuthenticationToken} using the provided parameters.
|
|
|
|
*
|
|
|
|
* @param code the authorization code
|
|
|
|
* @param clientPrincipal the authenticated client principal
|
|
|
|
* @param redirectUri the redirect uri
|
|
|
|
*/
|
2020-04-24 03:45:34 +07:00
|
|
|
public OAuth2AuthorizationCodeAuthenticationToken(String code,
|
|
|
|
Authentication clientPrincipal, @Nullable String redirectUri) {
|
|
|
|
super(Collections.emptyList());
|
2020-06-04 03:08:52 +07:00
|
|
|
Assert.hasText(code, "code cannot be empty");
|
|
|
|
Assert.notNull(clientPrincipal, "clientPrincipal cannot be null");
|
2020-04-24 03:45:34 +07:00
|
|
|
this.code = code;
|
|
|
|
this.clientPrincipal = clientPrincipal;
|
2020-05-25 20:23:18 +07:00
|
|
|
this.redirectUri = redirectUri;
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|
|
|
|
|
2020-06-04 03:08:52 +07:00
|
|
|
/**
|
|
|
|
* Constructs an {@code OAuth2AuthorizationCodeAuthenticationToken} using the provided parameters.
|
|
|
|
*
|
|
|
|
* @param code the authorization code
|
|
|
|
* @param clientId the client identifier
|
|
|
|
* @param redirectUri the redirect uri
|
|
|
|
*/
|
2020-04-24 03:45:34 +07:00
|
|
|
public OAuth2AuthorizationCodeAuthenticationToken(String code,
|
|
|
|
String clientId, @Nullable String redirectUri) {
|
|
|
|
super(Collections.emptyList());
|
2020-06-04 03:08:52 +07:00
|
|
|
Assert.hasText(code, "code cannot be empty");
|
|
|
|
Assert.hasText(clientId, "clientId cannot be empty");
|
2020-04-24 03:45:34 +07:00
|
|
|
this.code = code;
|
|
|
|
this.clientId = clientId;
|
2020-05-25 20:23:18 +07:00
|
|
|
this.redirectUri = redirectUri;
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-25 20:23:18 +07:00
|
|
|
public Object getPrincipal() {
|
|
|
|
return this.clientPrincipal != null ? this.clientPrincipal : this.clientId;
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-25 20:23:18 +07:00
|
|
|
public Object getCredentials() {
|
|
|
|
return "";
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|
2020-05-07 23:24:58 +07:00
|
|
|
|
|
|
|
/**
|
2020-06-04 03:08:52 +07:00
|
|
|
* Returns the authorization code.
|
2020-05-07 23:24:58 +07:00
|
|
|
*
|
2020-06-04 03:08:52 +07:00
|
|
|
* @return the authorization code
|
2020-05-07 23:24:58 +07:00
|
|
|
*/
|
|
|
|
public String getCode() {
|
|
|
|
return this.code;
|
|
|
|
}
|
2020-05-25 20:23:18 +07:00
|
|
|
|
|
|
|
/**
|
2020-06-04 03:08:52 +07:00
|
|
|
* Returns the redirect uri.
|
2020-05-25 20:23:18 +07:00
|
|
|
*
|
2020-06-04 03:08:52 +07:00
|
|
|
* @return the redirect uri
|
2020-05-25 20:23:18 +07:00
|
|
|
*/
|
2020-06-04 03:08:52 +07:00
|
|
|
public @Nullable String getRedirectUri() {
|
2020-05-25 20:23:18 +07:00
|
|
|
return this.redirectUri;
|
|
|
|
}
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|