Java源码示例:org.apache.oltu.oauth2.common.OAuth
示例1
public OAuthMessage applyOAuthParameters(OAuthMessage message, Map<String, Object> params) throws OAuthSystemException {
String messageUrl = message.getLocationUri();
if (messageUrl != null) {
StringBuilder url = new StringBuilder(messageUrl);
if (params.containsKey(OAuth.OAUTH_REFRESH_TOKEN)) {
params.remove(OAuth.OAUTH_REFRESH_TOKEN);
}
String fragmentQuery = OAuthUtils.format(params.entrySet(), "UTF-8");
if (!OAuthUtils.isEmpty(fragmentQuery)) {
if (params.size() > 0) {
url.append("#").append(fragmentQuery);
}
}
message.setLocationUri(url.toString());
}
return message;
}
示例2
/**
* Construct a WWW-Authenticate header
*/
public static String encodeOAuthHeader(Map<String, Object> entries) {
StringBuffer sb = new StringBuffer();
sb.append(OAuth.OAUTH_HEADER_NAME).append(" ");
for (Map.Entry<String, Object> entry : entries.entrySet()) {
String value = entry.getValue() == null? null: String.valueOf(entry.getValue());
if (!OAuthUtils.isEmpty(entry.getKey()) && !OAuthUtils.isEmpty(value)) {
sb.append(entry.getKey());
sb.append("=\"");
sb.append(value);
sb.append("\",");
}
}
return sb.substring(0, sb.length() - 1);
}
示例3
@Override
public void validateClientAuthenticationCredentials(T request) throws OAuthProblemException {
if (enforceClientAuthentication) {
Set<String> missingParameters = new HashSet<String>();
String clientAuthHeader = request.getHeader(OAuth.HeaderType.AUTHORIZATION);
String[] clientCreds = OAuthUtils.decodeClientAuthenticationHeader(clientAuthHeader);
// Only fallback to params if the auth header is not correct. Don't allow a mix of auth header vs params
if (clientCreds == null || OAuthUtils.isEmpty(clientCreds[0]) || OAuthUtils.isEmpty(clientCreds[1])) {
if (OAuthUtils.isEmpty(request.getParameter(OAuth.OAUTH_CLIENT_ID))) {
missingParameters.add(OAuth.OAUTH_CLIENT_ID);
}
if (OAuthUtils.isEmpty(request.getParameter(OAuth.OAUTH_CLIENT_SECRET))) {
missingParameters.add(OAuth.OAUTH_CLIENT_SECRET);
}
}
if (!missingParameters.isEmpty()) {
throw OAuthUtils.handleMissingParameters(missingParameters);
}
}
}
示例4
/**
* Constructs CarbonOAuthTokenRequest from the given HttpServletRequest
*
* @param request an instance of HttpServletRequest that represents an OAuth token request
* @throws OAuthSystemException
* @throws OAuthProblemException
*/
public CarbonOAuthTokenRequest(HttpServletRequest request) throws OAuthSystemException,
OAuthProblemException {
super(request);
assertion = request.getParameter(OAuth.OAUTH_ASSERTION);
windows_token = request.getParameter(OAuthConstants.WINDOWS_TOKEN);
tenantDomain = request.getParameter(MultitenantConstants.TENANT_DOMAIN);
if (tenantDomain == null) {
tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
// Store all request parameters
if (request.getParameterNames() != null) {
List<RequestParameter> requestParameterList = new ArrayList<RequestParameter>();
while (request.getParameterNames().hasMoreElements()) {
String key = request.getParameterNames().nextElement();
String value = request.getParameter(key);
requestParameterList.add(new RequestParameter(key, value));
}
requestParameters =
requestParameterList.toArray(new RequestParameter[requestParameterList.size()]);
}
}
示例5
/**
* Initialize a grant type validator
*
* @return an instance of OAuthValidator
* @throws OAuthProblemException
* @throws OAuthSystemException
*/
@Override
protected OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException {
String requestTypeValue = getParam(OAuth.OAUTH_GRANT_TYPE);
if (OAuthUtils.isEmpty(requestTypeValue)) {
throw OAuthUtils.handleOAuthProblemException("Missing grant_type parameter value");
}
Class<? extends OAuthValidator<HttpServletRequest>> clazz = OAuthServerConfiguration
.getInstance().getSupportedGrantTypeValidators().get(requestTypeValue);
if (clazz == null) {
if (log.isDebugEnabled()) {
//Do not change this log format as these logs use by external applications
log.debug("Unsupported Grant Type : " + requestTypeValue +
" for client id : " + getClientId());
}
throw OAuthUtils.handleOAuthProblemException("Invalid grant_type parameter value");
}
return OAuthUtils.instantiateClass(clazz);
}
示例6
protected OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException {
String responseTypeValue = getParam(OAuth.OAUTH_RESPONSE_TYPE);
if (OAuthUtils.isEmpty(responseTypeValue)) {
throw OAuthUtils.handleOAuthProblemException("Missing response_type parameter value");
}
Class<? extends OAuthValidator<HttpServletRequest>> clazz = OAuthServerConfiguration
.getInstance().getSupportedResponseTypeValidators().get(responseTypeValue);
if (clazz == null) {
if (log.isDebugEnabled()) {
//Do not change this log format as these logs use by external applications
log.debug("Unsupported Response Type : " + responseTypeValue +
" for client id : " + getClientId());
}
throw OAuthUtils.handleOAuthProblemException("Invalid response_type parameter value");
}
return OAuthUtils.instantiateClass(clazz);
}
示例7
private void setRequestBody(OAuthClientRequest request, String requestMethod, HttpURLConnection httpURLConnection)
throws IOException {
String requestBody = request.getBody();
if (OAuthUtils.isEmpty(requestBody)) {
return;
}
if (OAuth.HttpMethod.POST.equals(requestMethod) || OAuth.HttpMethod.PUT.equals(requestMethod)) {
httpURLConnection.setDoOutput(true);
OutputStream ost = httpURLConnection.getOutputStream();
PrintWriter pw = new PrintWriter(ost);
pw.print(requestBody);
pw.flush();
pw.close();
}
}
示例8
public void validateErrorResponse(OAuthClientResponse response) throws OAuthProblemException {
String error = response.getParam(OAuthError.OAUTH_ERROR);
if (!OAuthUtils.isEmpty(error)) {
String errorDesc = response.getParam(OAuthError.OAUTH_ERROR_DESCRIPTION);
String errorUri = response.getParam(OAuthError.OAUTH_ERROR_URI);
String state = response.getParam(OAuth.OAUTH_STATE);
throw OAuthProblemException.error(error).description(errorDesc).uri(errorUri).state(state);
}
}
示例9
public OAuthMessage applyOAuthParameters(OAuthMessage message, Map<String, Object> params)
throws OAuthSystemException {
String header = OAuthUtils.encodeAuthorizationBearerHeader(params);
message.addHeader(OAuth.HeaderType.AUTHORIZATION, header);
return message;
}
示例10
protected void setBody(String body) throws OAuthProblemException {
try {
this.body = body;
parameters = JSONUtils.parseJSON(body);
} catch (Throwable e) {
throw OAuthProblemException.error(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE,
"Invalid response! Response body is not " + OAuth.ContentType.JSON + " encoded");
}
}
示例11
public <T extends OAuthAccessTokenResponse> T accessToken(
OAuthClientRequest request,
Class<T> responseClass)
throws OAuthSystemException, OAuthProblemException {
return accessToken(request, OAuth.HttpMethod.POST, responseClass);
}
示例12
public <T extends OAuthAccessTokenResponse> T accessToken(
OAuthClientRequest request, String requestMethod, Class<T> responseClass)
throws OAuthSystemException, OAuthProblemException {
Map<String, String> headers = new HashMap<String, String>();
headers.put(OAuth.HeaderType.CONTENT_TYPE, OAuth.ContentType.URL_ENCODED);
return httpClient.execute(request, headers, requestMethod, responseClass);
}
示例13
public OAuthResponse buildQueryMessage() throws OAuthSystemException {
OAuthResponse msg = new OAuthResponse(location, responseCode);
this.applier = new QueryParameterApplier();
if (parameters.containsKey(OAuth.OAUTH_ACCESS_TOKEN)) {
this.applier = new FragmentParametersApplier();
}else{
this.applier = new QueryParameterApplier();
}
return (OAuthResponse)applier.applyOAuthParameters(msg, parameters);
}
示例14
public OAuthErrorResponseBuilder error(OAuthProblemException ex) {
this.parameters.put(OAuthError.OAUTH_ERROR, ex.getError());
this.parameters.put(OAuthError.OAUTH_ERROR_DESCRIPTION, ex.getDescription());
this.parameters.put(OAuthError.OAUTH_ERROR_URI, ex.getUri());
this.parameters.put(OAuth.OAUTH_STATE, ex.getState());
return this;
}
示例15
/**
* Return true if the given Content-Type header means FORM_ENCODED.
*/
public static boolean isFormEncoded(String contentType) {
if (contentType == null) {
return false;
}
int semi = contentType.indexOf(";");
if (semi >= 0) {
contentType = contentType.substring(0, semi);
}
return OAuth.ContentType.URL_ENCODED.equalsIgnoreCase(contentType.trim());
}
示例16
/**
* Construct an Authorization Bearer header
*/
public static String encodeAuthorizationBearerHeader(Map<String, Object> entries) {
StringBuffer sb = new StringBuffer();
sb.append(OAuth.OAUTH_HEADER_NAME).append(" ");
for (Map.Entry<String, Object> entry : entries.entrySet()) {
String value = entry.getValue() == null? null: String.valueOf(entry.getValue());
if (!OAuthUtils.isEmpty(entry.getKey()) && !OAuthUtils.isEmpty(value)) {
sb.append(value);
}
}
return sb.toString();
}
示例17
@Override
public void validateContentType(T request) throws OAuthProblemException {
String contentType = request.getContentType();
final String expectedContentType = OAuth.ContentType.URL_ENCODED;
if (!OAuthUtils.hasContentType(contentType, expectedContentType)) {
throw OAuthUtils.handleBadContentTypeException(expectedContentType);
}
}
示例18
@Override
public void validateMethod(HttpServletRequest request) throws OAuthProblemException {
String method = request.getMethod();
if (!OAuth.HttpMethod.GET.equals(method) && !OAuth.HttpMethod.POST.equals(method)) {
throw OAuthProblemException.error(OAuthError.CodeResponse.INVALID_REQUEST)
.description("Method not correct.");
}
}
示例19
@Override
public void validateMethod(HttpServletRequest request) throws OAuthProblemException {
String method = request.getMethod();
if (!OAuth.HttpMethod.GET.equals(method) && !OAuth.HttpMethod.POST.equals(method)) {
throw OAuthProblemException.error(OAuthError.CodeResponse.INVALID_REQUEST)
.description("Method not correct.");
}
}
示例20
private void fetchAndProcessToken(HttpServletRequest req, String code) throws
OAuthSystemException, OAuthProblemException, ApsSystemException {
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthClientRequest oAuthClientRequest = this.oidcHelper.buildOauthRequest(req, code);
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.resource(oAuthClientRequest, OAuth.HttpMethod.POST, OAuthJSONAccessTokenResponse.class);
_logger.info("----------------------TOKEN------------------- ");
String accessToken = oAuthResponse.getAccessToken();
_logger.info("accessToken -> " + accessToken);
UserDetails cdpUser = this.oidcHelper.getOidcUser(oAuthResponse.getAccessToken());
HttpSession session = req.getSession();
session.setAttribute(SystemConstants.SESSIONPARAM_CURRENT_USER, cdpUser);
}
示例21
public CodeTokenValidator() {
requiredParams.put(OAuth.OAUTH_CODE, new String[] {});
requiredParams.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {});
notAllowedParams.add(OAuth.OAUTH_ACCESS_TOKEN);
}
示例22
public OAuthBearerClientRequest setAccessToken(String accessToken) {
this.parameters.put(OAuth.OAUTH_BEARER_TOKEN, accessToken);
return this;
}
示例23
public AuthenticationRequestBuilder setResponseType(String type) {
this.parameters.put(OAuth.OAUTH_RESPONSE_TYPE, type);
return this;
}
示例24
public AuthenticationRequestBuilder setClientId(String clientId) {
this.parameters.put(OAuth.OAUTH_CLIENT_ID, clientId);
return this;
}
示例25
public AuthenticationRequestBuilder setRedirectURI(String uri) {
this.parameters.put(OAuth.OAUTH_REDIRECT_URI, uri);
return this;
}
示例26
public AuthenticationRequestBuilder setState(String state) {
this.parameters.put(OAuth.OAUTH_STATE, state);
return this;
}
示例27
public AuthenticationRequestBuilder setScope(String scope) {
this.parameters.put(OAuth.OAUTH_SCOPE, scope);
return this;
}
示例28
public TokenRequestBuilder setGrantType(GrantType grantType) {
this.parameters.put(OAuth.OAUTH_GRANT_TYPE, grantType == null ? null : grantType.toString());
return this;
}
示例29
public TokenRequestBuilder setClientId(String clientId) {
this.parameters.put(OAuth.OAUTH_CLIENT_ID, clientId);
return this;
}
示例30
public TokenRequestBuilder setClientSecret(String secret) {
this.parameters.put(OAuth.OAUTH_CLIENT_SECRET, secret);
return this;
}