Search in sources :

Example 36 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project dq-easy-cloud by dq-open-cloud.

the class EcAuthorizeController method userInfo.

@RequestMapping("/userInfo")
public HttpEntity userInfo(HttpServletRequest request) throws OAuthSystemException {
    try {
        // 构建OAuth资源请求
        OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request, ParameterStyle.QUERY);
        // 获取Access Token
        String accessToken = oauthRequest.getAccessToken();
        // 验证Access Token
        boolean accessTokenFlag = false;
        // if (!oAuthService.checkAccessToken(accessToken)) {
        if (accessTokenFlag) {
            // 如果不存在/过期了,返回未验证错误,需重新验证
            OAuthResponse oauthResponse = OAuthRSResponse.errorResponse(HttpServletResponse.SC_UNAUTHORIZED).setRealm("过期了").setError(OAuthError.ResourceResponse.INVALID_TOKEN).buildHeaderMessage();
            HttpHeaders headers = new HttpHeaders();
            headers.add(OAuth.HeaderType.WWW_AUTHENTICATE, oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE));
            return new ResponseEntity(headers, HttpStatus.UNAUTHORIZED);
        }
        // 返回用户名
        // String username = oAuthService.getUsernameByAccessToken(accessToken);
        Object username = tokenCache.get(accessToken);
        return new ResponseEntity(username, HttpStatus.OK);
    } catch (OAuthProblemException e) {
        // 检查是否设置了错误码
        String errorCode = e.getError();
        if (OAuthUtils.isEmpty(errorCode)) {
            OAuthResponse oauthResponse = OAuthRSResponse.errorResponse(HttpServletResponse.SC_UNAUTHORIZED).setRealm("server").buildHeaderMessage();
            HttpHeaders headers = new HttpHeaders();
            headers.add(OAuth.HeaderType.WWW_AUTHENTICATE, oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE));
            return new ResponseEntity(headers, HttpStatus.UNAUTHORIZED);
        }
        OAuthResponse oauthResponse = OAuthRSResponse.errorResponse(HttpServletResponse.SC_UNAUTHORIZED).setRealm("server").setError(e.getError()).setErrorDescription(e.getDescription()).setErrorUri(e.getUri()).buildHeaderMessage();
        HttpHeaders headers = new HttpHeaders();
        headers.add(OAuth.HeaderType.WWW_AUTHENTICATE, oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE));
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 37 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project BIMserver by opensourceBIM.

the class SendUrl method main.

public static void main(String[] args) {
    try {
        OAuthClientRequest request = OAuthClientRegistrationRequest.location("https://thisisanexperimentalserver.com/oauth/register/", OAuthRegistration.Type.PUSH).setName("Zapier").setUrl("https://zapier.com/dashboard/auth/oauth/return/App56192API").setDescription("App Description").setRedirectURL("https://zapier.com/dashboard/auth/oauth/return/App56192API").buildJSONMessage();
        OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new org.bimserver.webservices.impl.URLConnectionClient());
        OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
        System.out.println(response.getClientId());
        System.out.println(response.getClientSecret());
    } catch (OAuthSystemException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OAuthProblemException e) {
        e.printStackTrace();
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) IOException(java.io.IOException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 38 with OAuthProblemException

use of org.apache.oltu.oauth2.common.exception.OAuthProblemException in project BIMserver by opensourceBIM.

the class JsonHandler method getServiceMap.

private ServiceMap getServiceMap(HttpServletRequest httpRequest, BimServer bimServer, String methodName, String token, String oAuthCode) throws UserException {
    if (token == null) {
        token = httpRequest == null ? null : (String) httpRequest.getSession().getAttribute("token");
    }
    if (token == null) {
        token = oAuthCode;
    }
    if (token == null) {
        if (httpRequest != null) {
            try {
                OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(httpRequest, ParameterStyle.HEADER);
                token = oauthRequest.getAccessToken();
            } catch (OAuthSystemException e) {
            } catch (OAuthProblemException e) {
            }
        }
    }
    if (token == null) {
        return null;
    }
    ServiceMap serviceMap = bimServer.getServiceFactory().get(token, AccessMethod.JSON);
    return serviceMap;
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) ServiceMap(org.bimserver.webservices.ServiceMap) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Aggregations

OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)24 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)20 IOException (java.io.IOException)15 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)15 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)12 MediaType (okhttp3.MediaType)9 Request (okhttp3.Request)9 RequestBody (okhttp3.RequestBody)9 Response (okhttp3.Response)9 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)9 Builder (okhttp3.Request.Builder)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 URI (java.net.URI)6 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)5 OAuthAccessResourceRequest (org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest)5 OAuthIssuerImpl (org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl)4 OAuthAuthzResponse (org.apache.oltu.oauth2.client.response.OAuthAuthzResponse)4 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)4 AccessToken (io.github.tesla.authz.domain.AccessToken)3 ServletException (javax.servlet.ServletException)3