com.github.lontime.extpac4j.config.builder.OAuthExtBuilder Maven / Gradle / Ivy
The newest version!
package com.github.lontime.extpac4j.config.builder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.github.lontime.extjson.shaded.net.minidev.json.JSONValue;
import com.github.lontime.extpac4j.impl.BasicUserProfileReader;
import com.github.lontime.extpac4j.oauth.WechatExtClient;
import com.github.lontime.extpac4j.oauth.api.WechatExtApi20;
import org.pac4j.config.builder.OAuthBuilder;
import org.pac4j.core.client.Client;
import org.pac4j.oauth.client.WechatClient;
import org.pac4j.oauth.profile.wechat.WechatProfile;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.WECHAT_KEY;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.WECHAT_SCOPES;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.WECHAT_SECRET;
import static org.pac4j.core.util.CommonHelper.isNotBlank;
/**
* OAuthExtBuilder.
* @author lontime
* @since 1.0
*/
public class OAuthExtBuilder extends OAuthBuilder {
public OAuthExtBuilder(Map properties) {
super(properties);
}
public void tryCreateWechatClient(final List clients) {
final String key = getProperty(WECHAT_KEY);
final String secret = getProperty(WECHAT_SECRET);
final String scopes = getProperty(WECHAT_SCOPES);
boolean found = false;
if (isNotBlank(key) && isNotBlank(secret)) {
final WechatExtClient wechatClient = new WechatExtClient(key, secret);
if (isNotBlank(scopes)) {
wechatClient.setScopes(Arrays.stream(scopes.split(","))
.map(s -> s.trim().toUpperCase()).map(WechatClient.WechatScope::valueOf)
.collect(Collectors.toList()));
}
//wechatClient
wechatClient.getConfiguration().setApi(new WechatExtApi20());
wechatClient.setName(WechatClient.class.getSimpleName());
clients.add(wechatClient);
found = true;
}
if (found) {
JSONValue.registerReader(WechatProfile.class, new BasicUserProfileReader<>());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy