Search in sources :

Example 1 with ApplicationInfo

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

the class OAuth20Handler method filterClientApps.

List<ApplicationInfo> filterClientApps(FullHttpRequest req, List<ApplicationInfo> apps) {
    List<ApplicationInfo> filteredApps = new ArrayList<>();
    QueryStringDecoder dec = new QueryStringDecoder(req.uri());
    Map<String, List<String>> params = dec.parameters();
    if (params != null) {
        String status = QueryParameter.getFirstElement(params, "status");
        Integer statusInt;
        if (status != null && !status.isEmpty()) {
            try {
                statusInt = Integer.valueOf(status);
                for (ApplicationInfo app : apps) {
                    if (Objects.equals(app.getStatus(), statusInt)) {
                        filteredApps.add(app);
                    }
                }
            } catch (NumberFormatException e) {
                // status is invalid, ignore it
                filteredApps = Collections.unmodifiableList(apps);
            }
        } else {
            filteredApps = Collections.unmodifiableList(apps);
        }
    }
    return filteredApps;
}
Also used : ApplicationInfo(com.apifest.oauth20.bean.ApplicationInfo)

Example 2 with ApplicationInfo

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

the class ScopeService method getClientAppsByScope.

protected List<ApplicationInfo> getClientAppsByScope(String scopeName) {
    List<ApplicationInfo> scopeApps = new ArrayList<>();
    List<ApplicationInfo> allApps = DBManagerFactory.getInstance().getAllApplications().blockingGet();
    for (ApplicationInfo app : allApps) {
        if (app.getScope() != null && app.getScope().contains(scopeName)) {
            scopeApps.add(app);
            break;
        }
    }
    return scopeApps;
}
Also used : ApplicationInfo(com.apifest.oauth20.bean.ApplicationInfo) ArrayList(java.util.ArrayList)

Example 3 with ApplicationInfo

use of com.apifest.oauth20.bean.ApplicationInfo 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).blockingGet();
        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 4 with ApplicationInfo

use of com.apifest.oauth20.bean.ApplicationInfo 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).blockingGet();
    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).blockingGet();
            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 5 with ApplicationInfo

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

the class RedisDBManager method getAllApplications.

@Override
public Single<List<ApplicationInfo>> getAllApplications() {
    List<ApplicationInfo> list = new ArrayList<>();
    return CacheMapUtil.values(CLIENT_CREDENTIALS_KEY).switchIfEmpty(Single.just(new ArrayList<>())).map(values -> {
        for (String json : values) {
            ClientCredentials credentials = JSON.parseObject(json, ClientCredentials.class);
            ApplicationInfo app = ApplicationInfo.loadFromClientCredentials(credentials);
            if (app != null) {
                list.add(app);
            }
        }
        return list;
    });
}
Also used : ApplicationInfo(com.apifest.oauth20.bean.ApplicationInfo) ArrayList(java.util.ArrayList) ClientCredentials(com.apifest.oauth20.bean.ClientCredentials)

Aggregations

ApplicationInfo (com.apifest.oauth20.bean.ApplicationInfo)5 ArrayList (java.util.ArrayList)2 ClientCredentials (com.apifest.oauth20.bean.ClientCredentials)1 OAuthException (com.apifest.oauth20.bean.OAuthException)1 Scope (com.apifest.oauth20.bean.Scope)1 DocOAuth20Sub (info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)1 Matcher (java.util.regex.Matcher)1