AuthMeituanRequest.java 4.0 KB
Newer Older
1 2 3 4 5
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
6
import me.zhyd.oauth.config.AuthDefaultSource;
7 8 9 10 11 12 13
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
智布道's avatar
智布道 已提交
14
import me.zhyd.oauth.utils.HttpUtils;
15 16
import me.zhyd.oauth.utils.UrlBuilder;

17 18 19
import java.util.HashMap;
import java.util.Map;

20 21 22 23 24 25 26 27 28
/**
 * 美团登录
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @since 1.12.0
 */
public class AuthMeituanRequest extends AuthDefaultRequest {

    public AuthMeituanRequest(AuthConfig config) {
29
        super(config, AuthDefaultSource.MEITUAN);
30 31 32
    }

    public AuthMeituanRequest(AuthConfig config, AuthStateCache authStateCache) {
33
        super(config, AuthDefaultSource.MEITUAN, authStateCache);
34 35 36 37
    }

    @Override
    protected AuthToken getAccessToken(AuthCallback authCallback) {
38
        Map<String, String> form = new HashMap<>(7);
39 40 41 42 43
        form.put("app_id", config.getClientId());
        form.put("secret", config.getClientSecret());
        form.put("code", authCallback.getCode());
        form.put("grant_type", "authorization_code");

44
        String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, false).getBody();
45
        JSONObject object = JSONObject.parseObject(response);
46 47 48 49 50 51 52 53 54 55 56 57

        this.checkResponse(object);

        return AuthToken.builder()
            .accessToken(object.getString("access_token"))
            .refreshToken(object.getString("refresh_token"))
            .expireIn(object.getIntValue("expires_in"))
            .build();
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
58
        Map<String, String> form = new HashMap<>(5);
59 60 61 62
        form.put("app_id", config.getClientId());
        form.put("secret", config.getClientSecret());
        form.put("access_token", authToken.getAccessToken());

63
        String response = new HttpUtils(config.getHttpConfig()).post(source.userInfo(), form, false).getBody();
64
        JSONObject object = JSONObject.parseObject(response);
65 66 67 68

        this.checkResponse(object);

        return AuthUser.builder()
69
            .rawUserInfo(object)
70 71 72 73 74 75
            .uuid(object.getString("openid"))
            .username(object.getString("nickname"))
            .nickname(object.getString("nickname"))
            .avatar(object.getString("avatar"))
            .gender(AuthUserGender.UNKNOWN)
            .token(authToken)
76
            .source(source.toString())
77 78 79 80 81
            .build();
    }

    @Override
    public AuthResponse refresh(AuthToken oldToken) {
82
        Map<String, String> form = new HashMap<>(7);
83 84 85 86 87
        form.put("app_id", config.getClientId());
        form.put("secret", config.getClientSecret());
        form.put("refresh_token", oldToken.getRefreshToken());
        form.put("grant_type", "refresh_token");

88
        String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false).getBody();
89
        JSONObject object = JSONObject.parseObject(response);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

        this.checkResponse(object);

        return AuthResponse.builder()
            .code(AuthResponseStatus.SUCCESS.getCode())
            .data(AuthToken.builder()
                .accessToken(object.getString("access_token"))
                .refreshToken(object.getString("refresh_token"))
                .expireIn(object.getIntValue("expires_in"))
                .build())
            .build();
    }

    private void checkResponse(JSONObject object) {
        if (object.containsKey("error_code")) {
            throw new AuthException(object.getString("erroe_msg"));
        }
    }

    @Override
    public String authorize(String state) {
智布道's avatar
智布道 已提交
111
        return UrlBuilder.fromBaseUrl(super.authorize(state))
112 113 114 115 116
            .queryParam("scope", "")
            .build();
    }

}