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

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
6
import me.zhyd.oauth.config.AuthSource;
7
import me.zhyd.oauth.exception.AuthException;
8
import me.zhyd.oauth.model.AuthCallback;
9 10
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
智布道's avatar
智布道 已提交
11
import me.zhyd.oauth.model.AuthUserGender;
不合群的混子's avatar
不合群的混子 已提交
12
import me.zhyd.oauth.utils.UrlBuilder;
13 14 15 16 17 18 19 20

/**
 * Google登录
 *
 * @author yangkai.shen (https://xkcoding.com)
 * @version 1.3
 * @since 1.3
 */
智布道's avatar
智布道 已提交
21
public class AuthGoogleRequest extends AuthDefaultRequest {
22 23

    public AuthGoogleRequest(AuthConfig config) {
不合群的混子's avatar
不合群的混子 已提交
24
        super(config, AuthSource.GOOGLE);
25 26 27
    }

    @Override
28
    protected AuthToken getAccessToken(AuthCallback authCallback) {
不合群的混子's avatar
不合群的混子 已提交
29
        HttpResponse response = doPostAuthorizationCode(authCallback.getCode());
智布道's avatar
智布道 已提交
30
        JSONObject accessTokenObject = JSONObject.parseObject(response.body());
31

智布道's avatar
智布道 已提交
32 33
        if (accessTokenObject.containsKey("error") || accessTokenObject.containsKey("error_description")) {
            throw new AuthException("get google access_token has error:[" + accessTokenObject.getString("error") + "], error_description:[" + accessTokenObject
不合群的混子's avatar
不合群的混子 已提交
34
                .getString("error_description") + "]");
35 36 37
        }

        return AuthToken.builder()
不合群的混子's avatar
不合群的混子 已提交
38 39 40 41 42 43
            .accessToken(accessTokenObject.getString("access_token"))
            .expireIn(accessTokenObject.getIntValue("expires_in"))
            .scope(accessTokenObject.getString("scope"))
            .tokenType(accessTokenObject.getString("token_type"))
            .idToken(accessTokenObject.getString("id_token"))
            .build();
44 45 46 47
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
不合群的混子's avatar
不合群的混子 已提交
48
        HttpResponse response = doGetUserInfo(authToken);
49 50 51
        String userInfo = response.body();
        JSONObject object = JSONObject.parseObject(userInfo);
        return AuthUser.builder()
不合群的混子's avatar
不合群的混子 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
            .uuid(object.getString("sub"))
            .username(object.getString("name"))
            .avatar(object.getString("picture"))
            .nickname(object.getString("name"))
            .location(object.getString("locale"))
            .email(object.getString("email"))
            .gender(AuthUserGender.UNKNOWN)
            .token(authToken)
            .source(AuthSource.GOOGLE)
            .build();
    }

    /**
     * 返回认证url,可自行跳转页面
     *
     * @return 返回授权地址
     */
    @Override
    public String authorize() {
        return UrlBuilder.fromBaseUrl(source.authorize())
            .queryParam("response_type", "code")
            .queryParam("client_id", config.getClientId())
            .queryParam("scope", "openid%20email%20profile")
            .queryParam("redirect_uri", config.getRedirectUri())
            .queryParam("state", getRealState(config.getState()))
            .build();
    }

    /**
     * 返回获取userInfo的url
     *
     * @param authToken
     * @return 返回获取userInfo的url
     */
    @Override
    protected String userInfoUrl(AuthToken authToken) {
        return UrlBuilder.fromBaseUrl(source.userInfo()).queryParam("id_token", authToken.getAccessToken()).build();
89 90
    }
}