AuthRenrenUrlBuilder.java 2.0 KB
Newer Older
H
Hongwei Peng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
package me.zhyd.oauth.url;

import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponseStatus;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;

import java.text.MessageFormat;

import static me.zhyd.oauth.config.AuthSource.RENREN;

/**
 * 人人网相关的URL构建类
 *
 * @author hongwei.peng (pengisgood(at)gmail(dot)com)
 * @version 1.8.1
 * @since 1.8.1
 */
public class AuthRenrenUrlBuilder extends AuthDefaultUrlBuilder {

    private static final String RENREN_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}";
    private static final String RENREN_USER_INFO_PATTERN = "{0}?access_token={1}&userId={2}";
    private static final String RENREN_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}";
    private static final String RENREN_REFRESH_PATTERN = "{0}?refresh_token={1}&client_id={2}&client_secret={3}&grant_type=refresh_token";

    @Override
    public String getAccessTokenUrl(String code) {
        return MessageFormat.format(RENREN_ACCESS_TOKEN_PATTERN, RENREN.accessToken(), config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
    }

    @Override
    public String getUserInfoUrl(AuthUserInfoEntity userInfoEntity) {
        return MessageFormat.format(RENREN_USER_INFO_PATTERN, RENREN.userInfo(), userInfoEntity.getAccessToken(), userInfoEntity.getOpenId());
    }

    @Override
    public String getAuthorizeUrl() {
        return MessageFormat.format(RENREN_AUTHORIZE_PATTERN, RENREN.authorize(), config.getClientId(), config.getRedirectUri(), this.getRealState(config.getState()));
    }

    @Override
    public String getRefreshUrl(String refreshToken) {
        return MessageFormat.format(RENREN_REFRESH_PATTERN, RENREN.refresh(), refreshToken, config.getClientId(), config.getClientSecret());
    }

    @Override
    public String getRevokeUrl(String accessToken) {
        throw new AuthException(AuthResponseStatus.UNSUPPORTED);
    }
}