me.zhyd.oauth.model.AuthResponse Java Examples

The following examples show how to use me.zhyd.oauth.model.AuthResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthFeishuRequest.java    From JustAuth with MIT License 6 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken authToken) {
    JSONObject requestObject = new JSONObject();
    requestObject.put("app_id", config.getClientId());
    requestObject.put("app_secret", config.getClientSecret());
    requestObject.put("grant_type", "refresh_token");
    requestObject.put("refresh_token", authToken.getRefreshToken());
    String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), requestObject.toJSONString(), new HttpHeader()
        .add("Content-Type", "application/json"));
    JSONObject jsonObject = JSON.parseObject(response);
    this.checkResponse(jsonObject);
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(AuthToken.builder()
            .accessToken(jsonObject.getString("access_token"))
            .refreshToken(jsonObject.getString("refresh_token"))
            .expireIn(jsonObject.getIntValue("expires_in"))
            .tokenType(jsonObject.getString("token_type"))
            .openId(jsonObject.getString("open_id"))
            .build())
        .build();

}
 
Example #2
Source File: SocialSignOnProviderService.java    From MaxKey with Apache License 2.0 6 votes vote down vote up
public String getAccountId(String provider,AuthResponse<?> authResponse) {
	if(provider.equalsIgnoreCase("WeChatOpen")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("sinaweibo")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("qq")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("Alipay")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("Twitter")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("google")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("microsoft")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("Linkedin")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else if(provider.equalsIgnoreCase("DingTalk")) {
		return ((AuthUser)authResponse.getData()).getUuid();
	}else {
	    return ((AuthUser)authResponse.getData()).getUuid();
	}
}
 
Example #3
Source File: AuthServiceImpl.java    From OneBlog with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean login(String source, AuthCallback callback) {
    AuthRequest authRequest = RequestFactory.getInstance(source).getRequest();
    AuthResponse response = authRequest.login(callback);
    if (response.ok()) {
        AuthUser authUser = (AuthUser) response.getData();
        User newUser = BeanConvertUtil.doConvert(authUser, User.class);
        newUser.setSource(authUser.getSource().toString());
        if (null != authUser.getGender()) {
            newUser.setGender(authUser.getGender().getCode());
        }
        User user = userService.getByUuidAndSource(authUser.getUuid(), authUser.getSource().toString());
        newUser.setUserType(UserTypeEnum.USER);
        if (null != user) {
            newUser.setId(user.getId());
            userService.updateSelective(newUser);
        } else {
            userService.insert(newUser);
        }
        SessionUtil.setUser(newUser);
        return true;
    }
    log.warn("[{}] {}", source, response.getMsg());
    return false;
}
 
Example #4
Source File: AuthExtendRequestTest.java    From JustAuth with MIT License 6 votes vote down vote up
@Test
public void login() {
    AuthRequest request = new AuthExtendRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("http://redirectUri")
        .build());

    String state = AuthStateUtils.createState();
    request.authorize(state);
    AuthCallback callback = AuthCallback.builder()
        .code("code")
        .state(state)
        .build();
    AuthResponse response = request.login(callback);
    Assert.assertNotNull(response);

    AuthUser user = (AuthUser) response.getData();
    Assert.assertNotNull(user);
    System.out.println(JSON.toJSONString(user));
}
 
Example #5
Source File: AuthExtendRequest.java    From JustAuth with MIT License 6 votes vote down vote up
/**
 * 刷新access token (续期)
 *
 * @param authToken 登录成功后返回的Token信息
 * @return AuthResponse
 */
@Override
public AuthResponse refresh(AuthToken authToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(AuthToken.builder()
            .openId("openId")
            .expireIn(1000)
            .idToken("idToken")
            .scope("scope")
            .refreshToken("refreshToken")
            .accessToken("accessToken")
            .code("code")
            .build())
        .build();
}
 
Example #6
Source File: AuthMeituanRequest.java    From JustAuth with MIT License 6 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    Map<String, String> form = new HashMap<>(4);
    form.put("app_id", config.getClientId());
    form.put("secret", config.getClientSecret());
    form.put("refresh_token", oldToken.getRefreshToken());
    form.put("grant_type", "refresh_token");

    String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
    JSONObject object = JSONObject.parseObject(response);

    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();
}
 
Example #7
Source File: AuthTeambitionRequest.java    From JustAuth with MIT License 6 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    String uid = oldToken.getUid();
    String refreshToken = oldToken.getRefreshToken();

    Map<String, String> form = new HashMap<>(2);
    form.put("_userId", uid);
    form.put("refresh_token", refreshToken);
    String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
    JSONObject refreshTokenObject = JSONObject.parseObject(response);

    this.checkResponse(refreshTokenObject);

    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(AuthToken.builder()
            .accessToken(refreshTokenObject.getString("access_token"))
            .refreshToken(refreshTokenObject.getString("refresh_token"))
            .build())
        .build();
}
 
Example #8
Source File: AuthElemeRequest.java    From JustAuth with MIT License 6 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    Map<String, String> form = new HashMap<>(2);
    form.put("refresh_token", oldToken.getRefreshToken());
    form.put("grant_type", "refresh_token");

    HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_FORM, this.getRequestId(), true);
    String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, httpHeader, false);

    JSONObject object = JSONObject.parseObject(response);

    this.checkResponse(object);

    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(AuthToken.builder()
            .accessToken(object.getString("access_token"))
            .refreshToken(object.getString("refresh_token"))
            .tokenType(object.getString("token_type"))
            .expireIn(object.getIntValue("expires_in"))
            .build())
        .build();
}
 
Example #9
Source File: SocialLoginServiceImpl.java    From FEBS-Cloud with Apache License 2.0 6 votes vote down vote up
@Override
public FebsResponse resolveLogin(String oauthType, AuthCallback callback) throws FebsException {
    FebsResponse febsResponse = new FebsResponse();
    AuthRequest authRequest = factory.get(getAuthSource(oauthType));
    AuthResponse<?> response = authRequest.login(resolveAuthCallback(callback));
    if (response.ok()) {
        AuthUser authUser = (AuthUser) response.getData();
        UserConnection userConnection = userConnectionService.selectByCondition(authUser.getSource().toString(), authUser.getUuid());
        if (userConnection == null) {
            febsResponse.message(NOT_BIND).data(authUser);
        } else {
            SystemUser user = userManager.findByName(userConnection.getUserName());
            if (user == null) {
                throw new FebsException("系统中未找到与第三方账号对应的账户");
            }
            OAuth2AccessToken oAuth2AccessToken = getOauth2AccessToken(user);
            febsResponse.message(SOCIAL_LOGIN_SUCCESS).data(oAuth2AccessToken);
            febsResponse.put(USERNAME, user.getUsername());
        }
    } else {
        throw new FebsException(String.format("第三方登录失败,%s", response.getMsg()));
    }
    return febsResponse;
}
 
Example #10
Source File: AuthJdRequest.java    From JustAuth with MIT License 6 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    Map<String, String> params = new HashMap<>(5);
    params.put("app_key", config.getClientId());
    params.put("app_secret", config.getClientSecret());
    params.put("grant_type", "refresh_token");
    params.put("refresh_token", oldToken.getRefreshToken());
    String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), params, false);
    JSONObject object = JSONObject.parseObject(response);

    this.checkResponse(object);

    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(AuthToken.builder()
            .accessToken(object.getString("access_token"))
            .expireIn(object.getIntValue("expires_in"))
            .refreshToken(object.getString("refresh_token"))
            .scope(object.getString("scope"))
            .openId(object.getString("open_id"))
            .build())
        .build();
}
 
Example #11
Source File: AuthMiRequest.java    From JustAuth with MIT License 5 votes vote down vote up
/**
 * 刷新access token (续期)
 *
 * @param authToken 登录成功后返回的Token信息
 * @return AuthResponse
 */
@Override
public AuthResponse refresh(AuthToken authToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(getToken(refreshTokenUrl(authToken.getRefreshToken())))
        .build();
}
 
Example #12
Source File: OauthController.java    From cms with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/{type}/callback")
public void login(@PathVariable String type, String redirect, AuthCallback callback, HttpServletResponse response) throws IOException {
    AuthRequest authRequest = factory.get(type);
    AuthResponse authResponse = authRequest.login(callback);
    log.info("【response】= {}", JSONUtil.toJsonStr(authResponse));
    response.sendRedirect(redirect);
}
 
Example #13
Source File: OauthController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 登录成功后的回调
 *
 * @param oauthType 第三方登录类型
 * @param callback  携带返回的信息
 * @return 登录成功后的信息
 */
@RequestMapping("/{oauthType}/callback")
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
    AuthRequest authRequest = factory.get(getAuthSource(oauthType));
    AuthResponse response = authRequest.login(callback);
    log.info("【response】= {}", JSONUtil.toJsonStr(response));
    return response;
}
 
Example #14
Source File: OAuthController.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 收回授权
 *
 * @param source
 * @param token
 * @return
 * @throws IOException
 */
@RequestMapping("/revoke/{source}/{token}")
public ModelAndView revokeAuth(@PathVariable("source") String source, @PathVariable("token") String token) throws IOException {
    AuthRequest authRequest = RequestFactory.getInstance(source).getRequest();
    AuthResponse response = authRequest.revoke(AuthToken.builder().accessToken(token).build());
    if (response.getCode() == 2000) {
        return ResultUtil.redirect("/");
    }
    return ResultUtil.redirect("/login");
}
 
Example #15
Source File: AuthWeChatMpRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(this.getToken(refreshTokenUrl(oldToken.getRefreshToken())))
        .build();
}
 
Example #16
Source File: OAuth2Realm.java    From Shiro-Action with MIT License 5 votes vote down vote up
private AuthUser extractUserInfo(String code) {
    AuthRequest authRequest = oAuth2Helper.getAuthRequest(getAuthcTypeEnum());
    AuthResponse authResponse = authRequest.login(code);

    // 如果认证失败. 则输出日志, 并将用户重定向到错误页面.
    // 这里出错一般原因为程序的 OAuth2 ClientID 或 clientSecret 或 redirectUrl 配置错误.
    if (authResponse.getCode() == ResponseStatus.FAILURE.getCode()) {
        log.error(authResponse.getMsg());
        WebHelper.redirectUrl("/oauth2/error");
    }

    return (AuthUser) authResponse.getData();
}
 
Example #17
Source File: AuthExtendRequestTest.java    From JustAuth with MIT License 5 votes vote down vote up
@Test
public void refresh() {
    AuthRequest request = new AuthExtendRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("http://redirectUri")
        .build());

    AuthResponse response = request.refresh(AuthToken.builder().build());
    Assert.assertNotNull(response);
    System.out.println(JSON.toJSONString(response.getData()));

}
 
Example #18
Source File: AuthExtendRequestTest.java    From JustAuth with MIT License 5 votes vote down vote up
@Test
public void revoke() {
    AuthRequest request = new AuthExtendRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("http://redirectUri")
        .build());

    AuthResponse response = request.revoke(AuthToken.builder().build());
    Assert.assertNotNull(response);
    System.out.println(JSON.toJSONString(response));
}
 
Example #19
Source File: AuthExtendRequest.java    From JustAuth with MIT License 5 votes vote down vote up
/**
 * 撤销授权
 *
 * @param authToken 登录成功后返回的Token信息
 * @return AuthResponse
 */
@Override
public AuthResponse revoke(AuthToken authToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .msg(AuthResponseStatus.SUCCESS.getMsg())
        .build();
}
 
Example #20
Source File: AuthWeChatOpenRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(this.getToken(refreshTokenUrl(oldToken.getRefreshToken())))
        .build();
}
 
Example #21
Source File: AuthDouyinRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(getToken(refreshTokenUrl(oldToken.getRefreshToken())))
        .build();
}
 
Example #22
Source File: AuthBaiduRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken authToken) {
    String refreshUrl = UrlBuilder.fromBaseUrl(this.source.refresh())
        .queryParam("grant_type", "refresh_token")
        .queryParam("refresh_token", authToken.getRefreshToken())
        .queryParam("client_id", this.config.getClientId())
        .queryParam("client_secret", this.config.getClientSecret())
        .build();
    String response = new HttpUtils(config.getHttpConfig()).get(refreshUrl);
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(this.getAuthToken(response))
        .build();
}
 
Example #23
Source File: AuthBaiduRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse revoke(AuthToken authToken) {
    String response = doGetRevoke(authToken);
    JSONObject object = JSONObject.parseObject(response);
    this.checkResponse(object);
    // 返回1表示取消授权成功,否则失败
    AuthResponseStatus status = object.getIntValue("result") == 1 ? AuthResponseStatus.SUCCESS : AuthResponseStatus.FAILURE;
    return AuthResponse.builder().code(status.getCode()).msg(status.getMsg()).build();
}
 
Example #24
Source File: SocialLoginServiceImpl.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
@Override
public FebsResponse resolveBind(String oauthType, AuthCallback callback) throws FebsException {
    FebsResponse febsResponse = new FebsResponse();
    AuthRequest authRequest = factory.get(getAuthSource(oauthType));
    AuthResponse<?> response = authRequest.login(resolveAuthCallback(callback));
    if (response.ok()) {
        febsResponse.data(response.getData());
    } else {
        throw new FebsException(String.format("第三方登录失败,%s", response.getMsg()));
    }
    return febsResponse;
}
 
Example #25
Source File: AuthLinkedinRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse refresh(AuthToken oldToken) {
    String refreshToken = oldToken.getRefreshToken();
    if (StringUtils.isEmpty(refreshToken)) {
        throw new AuthException(AuthResponseStatus.REQUIRED_REFRESH_TOKEN, source);
    }
    String refreshTokenUrl = refreshTokenUrl(refreshToken);
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(this.getToken(refreshTokenUrl))
        .build();
}
 
Example #26
Source File: AuthMicrosoftRequest.java    From JustAuth with MIT License 5 votes vote down vote up
/**
 * 刷新access token (续期)
 *
 * @param authToken 登录成功后返回的Token信息
 * @return AuthResponse
 */
@Override
public AuthResponse refresh(AuthToken authToken) {
    return AuthResponse.builder()
        .code(AuthResponseStatus.SUCCESS.getCode())
        .data(getToken(refreshTokenUrl(authToken.getRefreshToken())))
        .build();
}
 
Example #27
Source File: AuthHuaweiRequest.java    From JustAuth with MIT License 5 votes vote down vote up
/**
 * 刷新access token (续期)
 *
 * @param authToken 登录成功后返回的Token信息
 * @return AuthResponse
 */
@Override
public AuthResponse refresh(AuthToken authToken) {
    Map<String, String> form = new HashMap<>(4);
    form.put("client_id", config.getClientId());
    form.put("client_secret", config.getClientSecret());
    form.put("refresh_token", authToken.getRefreshToken());
    form.put("grant_type", "refresh_token");

    String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
    return AuthResponse.builder().code(SUCCESS.getCode()).data(getAuthToken(response)).build();
}
 
Example #28
Source File: AuthWeiboRequest.java    From JustAuth with MIT License 5 votes vote down vote up
@Override
public AuthResponse revoke(AuthToken authToken) {
    String response = doGetRevoke(authToken);
    JSONObject object = JSONObject.parseObject(response);
    if (object.containsKey("error")) {
        return AuthResponse.builder()
            .code(AuthResponseStatus.FAILURE.getCode())
            .msg(object.getString("error"))
            .build();
    }
    // 返回 result = true 表示取消授权成功,否则失败
    AuthResponseStatus status = object.getBooleanValue("result") ? AuthResponseStatus.SUCCESS : AuthResponseStatus.FAILURE;
    return AuthResponse.builder().code(status.getCode()).msg(status.getMsg()).build();
}
 
Example #29
Source File: AuthDefaultRequest.java    From JustAuth with MIT License 5 votes vote down vote up
/**
 * 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
 *
 * @param e 具体的异常
 * @return AuthResponse
 */
private AuthResponse responseError(Exception e) {
    int errorCode = AuthResponseStatus.FAILURE.getCode();
    String errorMsg = e.getMessage();
    if (e instanceof AuthException) {
        AuthException authException = ((AuthException) e);
        errorCode = authException.getErrorCode();
        if (StringUtils.isNotEmpty(authException.getErrorMsg())) {
            errorMsg = authException.getErrorMsg();
        }
    }
    return AuthResponse.builder().code(errorCode).msg(errorMsg).build();
}
 
Example #30
Source File: AuthDefaultRequest.java    From JustAuth with MIT License 5 votes vote down vote up
/**
 * 统一的登录入口。当通过{@link AuthDefaultRequest#authorize(String)}授权成功后,会跳转到调用方的相关回调方法中
 * 方法的入参可以使用{@code AuthCallback},{@code AuthCallback}类中封装好了OAuth2授权回调所需要的参数
 *
 * @param authCallback 用于接收回调参数的实体
 * @return AuthResponse
 */
@Override
public AuthResponse login(AuthCallback authCallback) {
    try {
        AuthChecker.checkCode(source, authCallback);
        AuthChecker.checkState(authCallback.getState(), source, authStateCache);

        AuthToken authToken = this.getAccessToken(authCallback);
        AuthUser user = this.getUserInfo(authToken);
        return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(user).build();
    } catch (Exception e) {
        Log.error("Failed to login with oauth authorization.", e);
        return this.responseError(e);
    }
}