Skip to content

Commit

Permalink
move components from http to core
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Feb 17, 2016
1 parent f3b2f90 commit 4177abc
Show file tree
Hide file tree
Showing 103 changed files with 852 additions and 723 deletions.
5 changes: 0 additions & 5 deletions pac4j-cas/pom.xml
Expand Up @@ -36,11 +36,6 @@
<groupId>org.pac4j</groupId>
<artifactId>pac4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-http</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
Expand Down
Expand Up @@ -24,15 +24,12 @@
import org.pac4j.cas.profile.CasProfile;
import org.pac4j.cas.profile.HttpTGTProfile;
import org.pac4j.cas.util.HttpUtils;
import org.pac4j.core.client.DirectClient2;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.http.client.direct.DirectHttpClient;
import org.pac4j.http.credentials.UsernamePasswordCredentials;
import org.pac4j.http.credentials.authenticator.Authenticator;
import org.pac4j.http.credentials.authenticator.LocalCachingAuthenticator;
import org.pac4j.http.profile.creator.AuthenticatorProfileCreator;
import org.pac4j.core.credentials.UsernamePasswordCredentials;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.core.credentials.authenticator.LocalCachingAuthenticator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand All @@ -50,14 +47,9 @@
* @author Misagh Moayyed
* @since 1.8.0
*/
public abstract class AbstractCasRestClient extends DirectHttpClient<UsernamePasswordCredentials> {
public abstract class AbstractCasRestClient extends DirectClient2<UsernamePasswordCredentials, HttpTGTProfile> {

public AbstractCasRestClient(final Authenticator authenticator) {
setAuthenticator(authenticator);
setProfileCreator(new AuthenticatorProfileCreator<UsernamePasswordCredentials, CommonProfile>());
}

public void destroyTicketGrantingTicket(final WebContext context, final HttpTGTProfile profile) {
public void destroyTicketGrantingTicket(final HttpTGTProfile profile) {
HttpURLConnection connection = null;
try {
final URL endpointURL = new URL(getCasRestAuthenticator().getCasRestUrl());
Expand Down Expand Up @@ -124,6 +116,6 @@ public CasRestAuthenticator getCasRestAuthenticator() {
if (authenticator instanceof CasRestAuthenticator) {
return (CasRestAuthenticator) authenticator;
}
throw new TechnicalException("authenticator must be a CasRestAuthenticator (through a LocalCachingAuthenticator)");
throw new TechnicalException("authenticator must be a CasRestAuthenticator (or via a LocalCachingAuthenticator)");
}
}
Expand Up @@ -16,26 +16,65 @@
*/
package org.pac4j.cas.client.direct;

import org.pac4j.http.credentials.authenticator.Authenticator;
import org.pac4j.http.credentials.extractor.BasicAuthExtractor;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.core.credentials.extractor.BasicAuthExtractor;
import org.pac4j.core.util.CommonHelper;

/**
* CAS rest client that uses a {@link org.pac4j.http.credentials.extractor.BasicAuthExtractor}.
* Direct client which receives credentials as a basic auth and validates them via the CAS REST API.
*
* @author Misagh Moayyed
* @since 1.8.0
*/
public class CasRestBasicAuthClient extends AbstractCasRestClient {

private String headerName = HttpConstants.AUTHORIZATION_HEADER;

private String prefixHeader = HttpConstants.BASIC_HEADER_PREFIX;

public CasRestBasicAuthClient() {}

public CasRestBasicAuthClient(final Authenticator authenticator) {
super(authenticator);
this.extractor = new BasicAuthExtractor(CasRestBasicAuthClient.class.getSimpleName());
setAuthenticator(authenticator);
}

public CasRestBasicAuthClient(final Authenticator authenticator,
final String headerName, final String prefixHeader) {
super(authenticator);
this.extractor = new BasicAuthExtractor(headerName, prefixHeader,
CasRestBasicAuthClient.class.getSimpleName());
this(authenticator);
this.headerName = headerName;
this.prefixHeader = prefixHeader;
}

@Override
protected void internalInit(final WebContext context) {
CommonHelper.assertNotBlank("headerName", this.headerName);
CommonHelper.assertNotNull("prefixHeader", this.prefixHeader);
setExtractor(new BasicAuthExtractor(this.headerName, this.prefixHeader, getName()));
super.internalInit(context);
}

public String getHeaderName() {
return headerName;
}

public void setHeaderName(String headerName) {
this.headerName = headerName;
}

public String getPrefixHeader() {
return prefixHeader;
}

public void setPrefixHeader(String prefixHeader) {
this.prefixHeader = prefixHeader;
}

@Override
public String toString() {
return CommonHelper.toString(this.getClass(), "name", getName(), "headerName", this.headerName,
"prefixHeader", this.prefixHeader, "extractor", getExtractor(), "authenticator", getAuthenticator(),
"profileCreator", getProfileCreator());
}
}
Expand Up @@ -16,38 +16,65 @@
*/
package org.pac4j.cas.client.direct;

import org.pac4j.http.credentials.authenticator.Authenticator;
import org.pac4j.http.credentials.extractor.FormExtractor;
import org.pac4j.core.context.Pac4jConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.core.credentials.extractor.FormExtractor;
import org.pac4j.core.util.CommonHelper;

/**
* CAS rest client that uses a {@link FormExtractor}.
* Direct client which receives credentials as form parameters and validates them via the CAS REST API.
*
* @author Misagh Moayyed
* @since 1.8.0
*/
public class CasRestFormClient extends AbstractCasRestClient {

private String username = "username";
private String usernameParameter = Pac4jConstants.USERNAME;

private String password = "password";
private String passwordParameter = Pac4jConstants.PASSWORD;

public CasRestFormClient() {}

public CasRestFormClient(final Authenticator authenticator) {
super(authenticator);
this.extractor = new FormExtractor(username, password, CasRestFormClient.class.getSimpleName());
setAuthenticator(authenticator);
}

public CasRestFormClient(final Authenticator authenticator, final String usernameParameter, final String passwordParameter) {
this(authenticator);
this.usernameParameter = usernameParameter;
this.passwordParameter = passwordParameter;
}


@Override
protected void internalInit(final WebContext context) {
CommonHelper.assertNotBlank("usernameParameter", this.usernameParameter);
CommonHelper.assertNotBlank("passwordParameter", this.passwordParameter);
setExtractor(new FormExtractor(this.usernameParameter, this.passwordParameter, getName()));
super.internalInit(context);
}

public String getUsernameParameter() {
return usernameParameter;
}

public void setUsernameParameter(String usernameParameter) {
this.usernameParameter = usernameParameter;
}

public CasRestFormClient(final Authenticator authenticator, final String username, final String password) {
super(authenticator);
this.username = username;
this.password = password;
this.extractor = new FormExtractor(username, password, CasRestFormClient.class.getSimpleName());
public String getPasswordParameter() {
return passwordParameter;
}

public String getUsername() {
return username;
public void setPasswordParameter(String passwordParameter) {
this.passwordParameter = passwordParameter;
}

public String getPassword() {
return password;
@Override
public String toString() {
return CommonHelper.toString(this.getClass(), "name", getName(), "usernameParameter", this.usernameParameter,
"passwordParameter", this.passwordParameter, "extractor", getExtractor(), "authenticator", getAuthenticator(),
"profileCreator", getProfileCreator());
}
}
Expand Up @@ -26,6 +26,5 @@ public abstract class CasRestBasicAuthClient extends org.pac4j.cas.client.direct

public CasRestBasicAuthClient() {
super(null);
throw new UnsupportedOperationException("Use the org.pac4j.cas.client.direct.CasRestBasicAuthClient instead");
}
}
Expand Up @@ -26,6 +26,5 @@ public abstract class CasRestFormClient extends org.pac4j.cas.client.direct.CasR

public CasRestFormClient() {
super(null);
throw new UnsupportedOperationException("Use the org.pac4j.cas.client.direct.CasRestFormClient instead");
}
}
Expand Up @@ -38,7 +38,23 @@ public CasCredentials(final String serviceTicket, final String clientName) {
public String getServiceTicket() {
return this.serviceTicket;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

final CasCredentials that = (CasCredentials) o;

return !(serviceTicket != null ? !serviceTicket.equals(that.serviceTicket) : that.serviceTicket != null);

}

@Override
public int hashCode() {
return serviceTicket != null ? serviceTicket.hashCode() : 0;
}

@Override
public String toString() {
return CommonHelper.toString(this.getClass(), "serviceTicket", this.serviceTicket, "clientName",
Expand Down
Expand Up @@ -21,9 +21,12 @@
import org.pac4j.cas.util.HttpUtils;
import org.pac4j.cas.profile.HttpTGTProfile;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.http.credentials.UsernamePasswordCredentials;
import org.pac4j.http.credentials.authenticator.Authenticator;
import org.pac4j.core.credentials.UsernamePasswordCredentials;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.core.util.InitializableWebObject;

import java.io.BufferedWriter;
import java.io.IOException;
Expand All @@ -37,28 +40,34 @@
* @author Misagh Moayyed
* @since 1.8.0
*/
public class CasRestAuthenticator implements Authenticator<UsernamePasswordCredentials> {
private final String casServerPrefixUrl;
private final String casRestUrl;
public class CasRestAuthenticator extends InitializableWebObject implements Authenticator<UsernamePasswordCredentials> {

public CasRestAuthenticator(final String casServerPrefixUrl) {
this(casServerPrefixUrl, buildCasRestUrlFromCasServerPrefixUrl(casServerPrefixUrl));
}
private String casServerPrefixUrl;
private String casRestUrl;

private static String buildCasRestUrlFromCasServerPrefixUrl(final String casServerPrefixUrl) {
String restUrl = casServerPrefixUrl;
if (!restUrl.endsWith("/")) {
restUrl += "/";
}
restUrl += "v1/tickets";
return restUrl;
public CasRestAuthenticator() {}

public CasRestAuthenticator(final String casServerPrefixUrl) {
this.casServerPrefixUrl = casServerPrefixUrl;
}

public CasRestAuthenticator(final String casServerPrefixUrl, final String casRestUrl) {
this.casServerPrefixUrl = casServerPrefixUrl;
this.casRestUrl = casRestUrl;
}

@Override
protected void internalInit(final WebContext context) {
CommonHelper.assertNotBlank("casServerPrefixUrl", this.casServerPrefixUrl);
if (CommonHelper.isBlank(casRestUrl)) {
casRestUrl = casServerPrefixUrl;
if (!casRestUrl.endsWith("/")) {
casRestUrl += "/";
}
casRestUrl += "v1/tickets";
}
}

@Override
public void validate(final UsernamePasswordCredentials credentials) {
if (credentials == null || credentials.getPassword() == null || credentials.getUsername() == null) {
Expand Down Expand Up @@ -99,10 +108,18 @@ public String getCasServerPrefixUrl() {
return casServerPrefixUrl;
}

public void setCasServerPrefixUrl(String casServerPrefixUrl) {
this.casServerPrefixUrl = casServerPrefixUrl;
}

public String getCasRestUrl() {
return casRestUrl;
}

public void setCasRestUrl(String casRestUrl) {
this.casRestUrl = casRestUrl;
}

public TicketValidator getTicketValidator() {
return new Cas20ServiceTicketValidator(getCasServerPrefixUrl());
}
Expand Down
53 changes: 1 addition & 52 deletions pac4j-cas/src/main/java/org/pac4j/cas/profile/CasProfile.java
Expand Up @@ -20,58 +20,7 @@
/**
* <p>This class is the user profile for sites using CAS protocol.</p>
* <p>It is returned by the {@link org.pac4j.cas.client.CasClient}.</p>
* <table summary="" border="1" cellspacing="2px">
* <tr>
* <th>Method :</th>
* <th>From the JSON profile response :</th>
* </tr>
* <tr>
* <th colspan="2">The attributes of the {@link org.pac4j.core.profile.CommonProfile}</th>
* </tr>
* <tr>
* <td>String getEmail()</td>
* <td>the <i>email</i> attribute</td>
* </tr>
* <tr>
* <td>String getFirstName()</td>
* <td>the <i>first_name</i> attribute</td>
* </tr>
* <tr>
* <td>String getFamilyName()</td>
* <td>the <i>last_name</i> attribute</td>
* </tr>
* <tr>
* <td>String getDisplayName()</td>
* <td>the <i>display_name</i> attribute</td>
* </tr>
* <tr>
* <td>String getUsername()</td>
* <td>the <i>username</i> attribute</td>
* </tr>
* <tr>
* <td>Gender getGender()</td>
* <td>the <i>gender</i> attribute</td>
* </tr>
* <tr>
* <td>Locale getLocale()</td>
* <td>the <i>locale</i> attribute</td>
* </tr>
* <tr>
* <td>String getPictureUrl()</td>
* <td>the <i>picture_url</i> attribute</td>
* </tr>
* <tr>
* <td>String getProfileUrl()</td>
* <td>the <i>profile_url</i> attribute</td>
* </tr>
* <tr>
* <td>String getLocation()</td>
* <td>the <i>location</i> attribute</td>
* </tr>
* </table>
* <p>All other attributes must be retrieved using the {@link #getAttributes()} method.</p>
*
* @see org.pac4j.cas.client.CasClient
*
* @author Jerome Leleu
* @since 1.4.0
*/
Expand Down

0 comments on commit 4177abc

Please sign in to comment.