
org.pac4j.oauth.client.WordPressClient Maven / Gradle / Ivy
package org.pac4j.oauth.client;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.scribejava.core.builder.api.BaseApi;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.wordpress.WordPressAttributesDefinition;
import org.pac4j.oauth.profile.wordpress.WordPressProfile;
import org.pac4j.scribe.builder.api.WordPressApi20;
/**
* This class is the OAuth client to authenticate users in WordPress.
* It returns a {@link org.pac4j.oauth.profile.wordpress.WordPressProfile}.
* More information at http://developer.wordpress.com/docs/oauth2/
*
* @author Jerome Leleu
* @since 1.1.0
*/
public class WordPressClient extends BaseOAuth20Client {
public WordPressClient() {
setTokenAsHeader(true);
}
public WordPressClient(final String key, final String secret) {
setKey(key);
setSecret(secret);
setTokenAsHeader(true);
}
@Override
protected BaseApi getApi() {
return new WordPressApi20();
}
@Override
protected boolean hasOAuthGrantType() {
return true;
}
@Override
protected String getProfileUrl(final OAuth2AccessToken accessToken) {
return "https://public-api.wordpress.com/rest/v1/me/?pretty=1";
}
@Override
protected WordPressProfile extractUserProfile(final String body) throws HttpAction {
final WordPressProfile profile = new WordPressProfile();
JsonNode json = JsonHelper.getFirstNode(body);
if (json != null) {
profile.setId(JsonHelper.getElement(json, "ID"));
for (final String attribute : profile.getAttributesDefinition().getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
json = json.get("meta");
if (json != null) {
final String attribute = WordPressAttributesDefinition.LINKS;
profile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
}
return profile;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy