Search in sources :

Example 6 with DocOAuth20Sub

use of info.xiancloud.core.apidoc.annotation.DocOAuth20Sub in project xian by happyyangyuan.

the class OAuth20Handler method invokeExceptionHandler.

@DocOAuth20Sub(name = "invokeExceptionHandler", dec = "触发异常事件监听器的回调方法", method = "", url = "", args = { @DocOAuth20SubIn(name = "req", dec = "HTTP请求封装对象", require = true, type = FullHttpRequest.class) })
private void invokeExceptionHandler(Exception ex, FullHttpRequest request) {
    List<Class<? extends ExceptionEventHandler>> handlers = LifecycleEventHandlers.exceptionHandlers;
    for (int i = 0; i < handlers.size(); i++) {
        try {
            ExceptionEventHandler handler = handlers.get(i).newInstance();
            handler.handleException(ex, request);
        } catch (InstantiationException e) {
            throw new RuntimeException("cannot instantiate exception handler", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("cannot invoke exception handler", e);
        }
    }
}
Also used : ExceptionEventHandler(com.apifest.oauth20.api.ExceptionEventHandler) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)

Example 7 with DocOAuth20Sub

use of info.xiancloud.core.apidoc.annotation.DocOAuth20Sub in project xian by happyyangyuan.

the class OAuth20Handler method handleRegister.

@DocOAuth20Sub(name = "handleRegister", dec = "注册application", method = "POST", url = "/oauth2.0/applications", args = { @DocOAuth20SubIn(name = "name", dec = "application名称", require = true, type = String.class), @DocOAuth20SubIn(name = "scope", dec = "支持由空格分割的多个scope", require = true, type = String.class), @DocOAuth20SubIn(name = "redirect_uri", dec = "redirect_uri", require = true, type = String.class), @DocOAuth20SubIn(name = "client_id", dec = "client_id", require = false, type = String.class), @DocOAuth20SubIn(name = "client_secret", dec = "client_secret", require = false, type = String.class), @DocOAuth20SubIn(name = "description", dec = "用户自定义application描述", require = false, type = String.class), @DocOAuth20SubIn(name = "application_details", dec = "用户自定义的多个键值对", require = false, type = Map.class) })
FullHttpResponse handleRegister(FullHttpRequest req) {
    FullHttpResponse response = null;
    try {
        ClientCredentials creds = auth.issueClientCredentials(req);
        String jsonString = JSON.toJSONString(creds);
        LOG.info("credentials:" + jsonString);
        response = ResponseBuilder.createOkResponse(jsonString);
    } catch (OAuthException ex) {
        response = ResponseBuilder.createOAuthExceptionResponse(ex);
        invokeExceptionHandler(ex, req);
    } catch (Exception e1) {
        LOG.error("error handle register", e1);
        invokeExceptionHandler(e1, req);
    }
    if (response == null) {
        LOG.warn("response is null !", new Throwable());
        response = ResponseBuilder.createBadRequestResponse(ResponseBuilder.CANNOT_REGISTER_APP);
    }
    return response;
}
Also used : OAuthException(com.apifest.oauth20.bean.OAuthException) ClientCredentials(com.apifest.oauth20.bean.ClientCredentials) URISyntaxException(java.net.URISyntaxException) OAuthException(com.apifest.oauth20.bean.OAuthException) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)

Example 8 with DocOAuth20Sub

use of info.xiancloud.core.apidoc.annotation.DocOAuth20Sub in project xian by happyyangyuan.

the class OAuth20Handler method handleGetScope.

@DocOAuth20Sub(name = "handleGetScope", dec = "获取单个scope", method = "GET", url = "/oauth2.0/scopes/{scopeName}", args = { @DocOAuth20SubIn(name = "scope", dec = "scope name", require = true, type = String.class) })
private FullHttpResponse handleGetScope(FullHttpRequest req) {
    FullHttpResponse response;
    Matcher m = OAUTH_CLIENT_SCOPE_PATTERN.matcher(req.uri());
    if (m.find()) {
        String scopeName = m.group(1);
        ScopeService scopeService = getScopeService();
        try {
            String responseMsg = scopeService.getScopeByName(scopeName);
            response = ResponseBuilder.createOkResponse(responseMsg);
        } catch (OAuthException e) {
            invokeExceptionHandler(e, req);
            response = ResponseBuilder.createResponse(e.getHttpStatus(), e.getMessage());
        }
    } else {
        response = ResponseBuilder.createNotFoundResponse();
    }
    return response;
}
Also used : Matcher(java.util.regex.Matcher) OAuthException(com.apifest.oauth20.bean.OAuthException) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)

Example 9 with DocOAuth20Sub

use of info.xiancloud.core.apidoc.annotation.DocOAuth20Sub in project xian by happyyangyuan.

the class OAuth20Handler method handleGetClientApplication.

@DocOAuth20Sub(name = "handleGetClientApplication", dec = "获取单个application相关信息", method = "GET", url = "/oauth2.0/applications/{LOCAL_NODE_ID}", args = { @DocOAuth20SubIn(name = "client_id", dec = "client_id", require = true, type = String.class) })
FullHttpResponse handleGetClientApplication(FullHttpRequest req) {
    FullHttpResponse response;
    Matcher m = APPLICATION_PATTERN.matcher(req.uri());
    if (m.find()) {
        String clientId = m.group(1);
        ApplicationInfo appInfo = auth.getApplicationInfo(clientId);
        if (appInfo != null) {
            String json = JSON.toJSONString(appInfo);
            LOG.debug(json);
            response = ResponseBuilder.createOkResponse(json);
        } else {
            response = ResponseBuilder.createResponse(HttpResponseStatus.NOT_FOUND, ResponseBuilder.CLIENT_APP_NOT_EXIST);
        }
    } else {
        response = ResponseBuilder.createNotFoundResponse();
    }
    return response;
}
Also used : Matcher(java.util.regex.Matcher) ApplicationInfo(com.apifest.oauth20.bean.ApplicationInfo) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)

Example 10 with DocOAuth20Sub

use of info.xiancloud.core.apidoc.annotation.DocOAuth20Sub in project xian by happyyangyuan.

the class OAuth20Handler method handleAuthorize.

@DocOAuth20Sub(name = "handleAuthorize", dec = "获取code", method = "GET", url = "/oauth2.0/auth-codes", args = { @DocOAuth20SubIn(name = "response_type", dec = "response_type仅支持code类型", require = true, type = String.class), @DocOAuth20SubIn(name = "client_id", dec = "client_id", require = true, type = String.class), @DocOAuth20SubIn(name = "state", dec = "state为用户自定义内容,重定向时会带上该参数", require = false, type = String.class), @DocOAuth20SubIn(name = "redirect_uri", dec = "redirect_uri", require = true, type = String.class), @DocOAuth20SubIn(name = "user_id", dec = "用户自定义值", require = false, type = String.class), @DocOAuth20SubIn(name = "scope", dec = "支持由空格分割的多个scope", require = true, type = String.class) })
private FullHttpResponse handleAuthorize(FullHttpRequest req) {
    FullHttpResponse response;
    try {
        String redirectURI = auth.issueAuthorizationCode(req);
        // TODO: validation http protocol?
        LOG.info(String.format("redirectURI: %s", redirectURI));
        // return auth_code
        response = ResponseBuilder.createOkResponse(new JSONObject() {

            {
                put("redirect_uri", redirectURI);
            }
        }.toString());
    /*accessTokensLog.info("authCode " + response.content().toString(CharsetUtil.UTF_8));*/
    } catch (OAuthException ex) {
        response = ResponseBuilder.createOAuthExceptionResponse(ex);
        invokeExceptionHandler(ex, req);
    }
    return response;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) OAuthException(com.apifest.oauth20.bean.OAuthException) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)

Aggregations

DocOAuth20Sub (info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)11 OAuthException (com.apifest.oauth20.bean.OAuthException)7 Matcher (java.util.regex.Matcher)5 AccessToken (info.xiancloud.core.support.authen.AccessToken)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ExceptionEventHandler (com.apifest.oauth20.api.ExceptionEventHandler)1 ApplicationInfo (com.apifest.oauth20.bean.ApplicationInfo)1 ClientCredentials (com.apifest.oauth20.bean.ClientCredentials)1 DocOAuth20SubIn (info.xiancloud.core.apidoc.annotation.DocOAuth20SubIn)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1