Search in sources :

Example 6 with AccessToken

use of info.xiancloud.core.support.authen.AccessToken in project xian by happyyangyuan.

the class OAuth20Handler method handlePostAccessToken.

@DocOAuth20Sub(name = "handlePostAccessToken", dec = "获取新access_token", method = "POST", url = "/oauth2.0/tokens", args = { @DocOAuth20SubIn(name = "grant_type", dec = "grant_type有四种类型,分别为authorization_code,refresh_token,client_credentials,password", require = true, type = String.class), @DocOAuth20SubIn(name = "client_id", dec = "client_id", require = true, type = String.class), @DocOAuth20SubIn(name = "client_secret", dec = "client_secret", require = true, type = String.class), @DocOAuth20SubIn(name = "redirect_uri", dec = "仅当grant_type为authorization_code时必填", require = false, type = String.class), @DocOAuth20SubIn(name = "code", dec = "仅当grant_type为authorization_code时必填", require = false, type = String.class), @DocOAuth20SubIn(name = "refresh_token", dec = "仅当grant_type为refresh_token时必填", require = false, type = String.class), @DocOAuth20SubIn(name = "scope", dec = "仅当grant_type为refresh_token,client_credentials时填写有效", require = false, type = String.class), @DocOAuth20SubIn(name = "username", dec = "仅当grant_type为password时必填", require = false, type = String.class), @DocOAuth20SubIn(name = "password", dec = "仅当grant_type为password时必填", require = false, type = String.class) })
FullHttpResponse handlePostAccessToken(FullHttpRequest request) {
    FullHttpResponse response = null;
    String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
    if (contentType != null && (contentType.contains(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED) || contentType.contains(HttpHeaderValues.APPLICATION_JSON))) {
        try {
            AccessToken accessToken = auth.issueAccessToken(request);
            if (accessToken != null) {
                String jsonString = JSON.toJSONString(accessToken);
                LOG.debug("access token:" + jsonString);
                response = ResponseBuilder.createOkResponse(jsonString);
            /*accessTokensLog.debug(String.format("token {%s}", jsonString));*/
            }
        } catch (OAuthException ex) {
            response = ResponseBuilder.createOAuthExceptionResponse(ex);
            invokeExceptionHandler(ex, request);
        }
        if (response == null) {
            response = ResponseBuilder.createBadRequestResponse(ResponseBuilder.CANNOT_ISSUE_TOKEN);
        }
    } else {
        response = ResponseBuilder.createResponse(HttpResponseStatus.BAD_REQUEST, ResponseBuilder.UNSUPPORTED_MEDIA_TYPE);
    }
    return response;
}
Also used : AccessToken(info.xiancloud.core.support.authen.AccessToken) OAuthException(com.apifest.oauth20.bean.OAuthException) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)

Example 7 with AccessToken

use of info.xiancloud.core.support.authen.AccessToken in project xian by happyyangyuan.

the class Authenticator method revokeToken.

public boolean revokeToken(FullHttpRequest req) throws OAuthException {
    RevokeTokenRequest revokeRequest = new RevokeTokenRequest(req);
    revokeRequest.checkMandatoryParams();
    String clientId = revokeRequest.getClientId();
    // check valid client_id, status does not matter as token of inactive client app could be revoked too
    if (!isExistingClient(clientId)) {
        throw new OAuthException(ResponseBuilder.INVALID_CLIENT_ID, HttpResponseStatus.BAD_REQUEST);
    }
    String token = revokeRequest.getAccessToken();
    AccessToken accessToken = db.findAccessToken(token);
    if (accessToken != null) {
        if (accessToken.tokenExpired()) {
            LOG.info(String.format("access token {%s} is expired", token));
            return true;
        }
        if (clientId.equals(accessToken.getClientId())) {
            db.removeAccessToken(accessToken.getToken());
            LOG.info(String.format("access token {%s} set status invalid", token));
            return true;
        } else {
            LOG.info(String.format("access token {%s} is not obtained for that LOCAL_NODE_ID {%s}", token, clientId));
            return false;
        }
    }
    LOG.info(String.format("access token {%s} not found", token));
    return false;
}
Also used : AccessToken(info.xiancloud.core.support.authen.AccessToken)

Example 8 with AccessToken

use of info.xiancloud.core.support.authen.AccessToken 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

AccessToken (info.xiancloud.core.support.authen.AccessToken)8 OAuthException (com.apifest.oauth20.bean.OAuthException)2 DocOAuth20Sub (info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AuthenticationException (com.apifest.oauth20.api.AuthenticationException)1 UserDetails (com.apifest.oauth20.api.UserDetails)1 TokenRequest (com.apifest.oauth20.bean.token_request.TokenRequest)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1