Search in sources :

Example 16 with Scope

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

the class ScopeService method deleteScope.

/**
 * Deletes a scope. If the scope does not exists, returns an error.
 *
 * @param scopeName scopeName
 * @return String message that will be returned in the response
 */
public String deleteScope(String scopeName) throws OAuthException {
    String responseMsg = "";
    Scope foundScope = DBManagerFactory.getInstance().findScope(scopeName);
    if (foundScope == null) {
        LOG.error("scope does not exist");
        throw new OAuthException(SCOPE_NOT_EXIST, HttpResponseStatus.BAD_REQUEST);
    } else {
        // first, check whether there is a client app registered with that scope
        List<ApplicationInfo> registeredApps = getClientAppsByScope(scopeName);
        if (registeredApps.size() > 0) {
            responseMsg = SCOPE_USED_BY_APP_MESSAGE;
        } else {
            boolean ok = DBManagerFactory.getInstance().deleteScope(scopeName);
            if (ok) {
                responseMsg = SCOPE_DELETED_OK_MESSAGE;
            } else {
                responseMsg = SCOPE_DELETED_NOK_MESSAGE;
            }
        }
    }
    return responseMsg;
}
Also used : Scope(com.apifest.oauth20.bean.Scope) OAuthException(com.apifest.oauth20.bean.OAuthException) ApplicationInfo(com.apifest.oauth20.bean.ApplicationInfo)

Example 17 with Scope

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

the class IssueAccessToken method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    JSONObject json = new JSONObject() {

        {
            put("client_id", msg.getString("appId"));
            put("client_secret", msg.getString("appSecret"));
            put("grant_type", "client_credentials");
        }
    };
    String body = json.toJSONString(), uri = msg.getString("$url");
    ByteBuf byteBuffer = Unpooled.wrappedBuffer(body.getBytes());
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri, byteBuffer);
    try {
        AccessToken token = OAuthService.auth.issueAccessToken(request);
        return UnitResponse.success(new JSONObject() {

            {
                put("appId", msg.getString("appId"));
                put("accessToken", token.getToken());
                put("valid", token.isValid());
                put("expiresIn", token.getExpiresIn());
                put("created", token.getCreated());
                put("scope", token.getScope());
            }
        });
    } catch (OAuthException e) {
        return UnitResponse.exception(e);
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) JSONObject(com.alibaba.fastjson.JSONObject) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AccessToken(info.xiancloud.core.support.authen.AccessToken) OAuthException(com.apifest.oauth20.bean.OAuthException) ByteBuf(io.netty.buffer.ByteBuf)

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