Commit 4f13a3f5 authored by Quxl's avatar Quxl

x

parent 4f6c943d
package com.egolm.sso.api;
public class AccessToken {
private String accessToken;
private String refreshToken;
private String idToken;
private String tokenType;
private Long expiresIn;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public String getIdToken() {
return idToken;
}
public void setIdToken(String idToken) {
this.idToken = idToken;
}
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public Long getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Long expiresIn) {
this.expiresIn = expiresIn;
}
}
package com.egolm.sso.api;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
public interface Auth2Api {
OAuthConfig getOAuthConfig();
default void toLoginPage(HttpServletResponse response) throws IOException, OAuthSystemException {
OAuthConfig config = this.getOAuthConfig();
AuthenticationRequestBuilder builder = OAuthClientRequest.authorizationLocation(config.getAuthorizeUrl());
builder.setResponseType(OAuth.OAUTH_CODE);
builder.setClientId(config.getClientId());
builder.setRedirectURI(config.getRediretUrl());
builder.setScope(config.getClientScope());
OAuthClientRequest oauthResponse = builder.buildQueryMessage();
response.sendRedirect(oauthResponse.getLocationUri());
}
default AccessToken getToken(HttpServletRequest request) throws OAuthProblemException, OAuthSystemException {
OAuthConfig config = this.getOAuthConfig();
OAuthAuthzResponse oauthAuthzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
String code = oauthAuthzResponse.getCode();
TokenRequestBuilder builder = OAuthClientRequest.tokenLocation(config.getAccessTokenUrl());
builder.setGrantType(GrantType.AUTHORIZATION_CODE);
builder.setClientId(config.getClientId());
builder.setClientSecret(config.getClientSecret());
builder.setRedirectURI(config.getRediretUrl());
builder.setCode(code);
OAuthClientRequest oauthClientRequest = builder.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oauthClientRequest, OAuth.HttpMethod.POST);
String accessToken = oAuthResponse.getAccessToken();
String refreshToken = oAuthResponse.getRefreshToken();
Long expiresIn = oAuthResponse.getExpiresIn();
String idToken = oAuthResponse.getParam("id_token");
String tokenType = oAuthResponse.getTokenType();
AccessToken tokenObj = new AccessToken();
tokenObj.setAccessToken(accessToken);
tokenObj.setRefreshToken(refreshToken);
tokenObj.setIdToken(idToken);
tokenObj.setTokenType(tokenType);
tokenObj.setExpiresIn(expiresIn);
return tokenObj;
}
}
package com.egolm.sso.api;
public class OAuthConfig {
private String clientId;
private String clientSecret;
private String clientScope;
private String rediretUrl;
private String authorizeUrl;
private String accessTokenUrl;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getClientScope() {
return clientScope;
}
public void setClientScope(String clientScope) {
this.clientScope = clientScope;
}
public String getAuthorizeUrl() {
return authorizeUrl;
}
public void setAuthorizeUrl(String authorizeUrl) {
this.authorizeUrl = authorizeUrl;
}
public String getAccessTokenUrl() {
return accessTokenUrl;
}
public void setAccessTokenUrl(String accessTokenUrl) {
this.accessTokenUrl = accessTokenUrl;
}
public String getRediretUrl() {
return rediretUrl;
}
public void setRediretUrl(String rediretUrl) {
this.rediretUrl = rediretUrl;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment