Search in sources :

Example 6 with Scope

use of com.apifest.oauth20.bean.Scope in project xian by happyyangyuan.

the class ScopeService method getScopeByName.

public String getScopeByName(String scopeName) throws OAuthException {
    String jsonString = null;
    Scope scope = DBManagerFactory.getInstance().findScope(scopeName);
    if (scope != null) {
        try {
            jsonString = JSON.toJSONString(scope);
        } catch (Exception e) {
            LOG.error("cannot load scopes", e);
            throw new OAuthException(e, null, HttpResponseStatus.BAD_REQUEST);
        }
    } else {
        throw new OAuthException(SCOPE_NOT_EXIST, HttpResponseStatus.NOT_FOUND);
    }
    return jsonString;
}
Also used : Scope(com.apifest.oauth20.bean.Scope) OAuthException(com.apifest.oauth20.bean.OAuthException) OAuthException(com.apifest.oauth20.bean.OAuthException) IOException(java.io.IOException)

Example 7 with Scope

use of com.apifest.oauth20.bean.Scope 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 Scope

use of com.apifest.oauth20.bean.Scope 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 Scope

use of com.apifest.oauth20.bean.Scope 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)

Example 10 with Scope

use of com.apifest.oauth20.bean.Scope in project xian by happyyangyuan.

the class OAuth20Handler method handleDeleteScope.

@DocOAuth20Sub(name = "handleDeleteScope", dec = "删除单个scope", method = "DELETE", url = "/oauth2.0/scopes/{scopeName}", args = { @DocOAuth20SubIn(name = "scope", dec = "scope name", require = true, type = String.class) })
FullHttpResponse handleDeleteScope(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.deleteScope(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)

Aggregations

OAuthException (com.apifest.oauth20.bean.OAuthException)14 Scope (com.apifest.oauth20.bean.Scope)8 DocOAuth20Sub (info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)7 IOException (java.io.IOException)5 Matcher (java.util.regex.Matcher)4 AccessToken (info.xiancloud.core.support.authen.AccessToken)3 ArrayList (java.util.ArrayList)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ClientCredentials (com.apifest.oauth20.bean.ClientCredentials)2 AuthenticationException (com.apifest.oauth20.api.AuthenticationException)1 UserDetails (com.apifest.oauth20.api.UserDetails)1 ApplicationInfo (com.apifest.oauth20.bean.ApplicationInfo)1 TokenRequest (com.apifest.oauth20.bean.token_request.TokenRequest)1 DBManager (com.apifest.oauth20.persistence.DBManager)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1