AuthMicrosoftRequest.java 3.8 KB
Newer Older
1 2 3 4 5 6 7
package me.zhyd.oauth.request;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
8
import me.zhyd.oauth.config.AuthSource;
9
import me.zhyd.oauth.exception.AuthException;
10
import me.zhyd.oauth.model.*;
智布道's avatar
智布道 已提交
11
import me.zhyd.oauth.url.AuthMicrosoftUrlBuilder;
12
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
13 14 15 16 17 18 19 20 21 22 23

import java.util.HashMap;
import java.util.Map;

/**
 * 微软登录
 *
 * @author yangkai.shen (https://xkcoding.com)
 * @version 1.5
 * @since 1.5
 */
智布道's avatar
智布道 已提交
24
public class AuthMicrosoftRequest extends AuthDefaultRequest {
25
    public AuthMicrosoftRequest(AuthConfig config) {
智布道's avatar
智布道 已提交
26
        super(config, AuthSource.MICROSOFT, new AuthMicrosoftUrlBuilder());
27 28 29
    }

    @Override
30
    protected AuthToken getAccessToken(AuthCallback authCallback) {
智布道's avatar
智布道 已提交
31
        String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode());
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

        return getToken(accessTokenUrl);
    }

    /**
     * 获取token,适用于获取access_token和刷新token
     *
     * @param accessTokenUrl 实际请求token的地址
     * @return token对象
     */
    private AuthToken getToken(String accessTokenUrl) {
        Map<String, Object> paramMap = new HashMap<>(6);
        HttpUtil.decodeParamMap(accessTokenUrl, "UTF-8").forEach(paramMap::put);
        HttpResponse response = HttpRequest.post(accessTokenUrl)
                .header("Host", "https://login.microsoftonline.com")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .form(paramMap)
                .execute();
        String accessTokenStr = response.body();
智布道's avatar
智布道 已提交
51
        JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
52

智布道's avatar
智布道 已提交
53
        this.checkResponse(accessTokenObject);
54 55

        return AuthToken.builder()
智布道's avatar
智布道 已提交
56 57 58 59 60
                .accessToken(accessTokenObject.getString("access_token"))
                .expireIn(accessTokenObject.getIntValue("expires_in"))
                .scope(accessTokenObject.getString("scope"))
                .tokenType(accessTokenObject.getString("token_type"))
                .refreshToken(accessTokenObject.getString("refresh_token"))
61 62 63 64 65 66 67 68 69 70 71 72 73 74
                .build();
    }

    private void checkResponse(JSONObject response) {
        if (response.containsKey("error")) {
            throw new AuthException(response.getString("error_description"));
        }
    }

    @Override
    protected AuthUser getUserInfo(AuthToken authToken) {
        String token = authToken.getAccessToken();
        String tokenType = authToken.getTokenType();
        String jwt = tokenType + " " + token;
75
        HttpResponse response = HttpRequest.get(this.urlBuilder.getUserInfoUrl(AuthUserInfoEntity.builder().build()))
76 77 78 79 80 81 82 83 84 85
                .header("Authorization", jwt)
                .execute();
        String userInfo = response.body();
        JSONObject object = JSONObject.parseObject(userInfo);
        return AuthUser.builder()
                .uuid(object.getString("id"))
                .username(object.getString("userPrincipalName"))
                .nickname(object.getString("displayName"))
                .location(object.getString("officeLocation"))
                .email(object.getString("mail"))
H
Hongwei Peng 已提交
86
                .gender(AuthUserGender.UNKNOWN)
87 88 89 90 91 92 93 94 95 96 97 98 99
                .token(authToken)
                .source(AuthSource.MICROSOFT)
                .build();
    }

    /**
     * 刷新access token (续期)
     *
     * @param authToken 登录成功后返回的Token信息
     * @return AuthResponse
     */
    @Override
    public AuthResponse refresh(AuthToken authToken) {
智布道's avatar
智布道 已提交
100
        String refreshTokenUrl = this.urlBuilder.getRefreshUrl(authToken.getRefreshToken());
101

智布道's avatar
智布道 已提交
102
        return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getToken(refreshTokenUrl)).build();
103 104
    }
}