Skip to content

Commit

Permalink
Merge pull request #249 from andamian/CADC-12837
Browse files Browse the repository at this point in the history
Modified OpenIdPrincipal to work with different issuers
  • Loading branch information
pdowler authored Jul 10, 2024
2 parents 925860a + b2ec1bb commit 8e7afb6
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cadc-util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sourceCompatibility = 1.8

group = 'org.opencadc'

version = '1.11.2'
version = '1.11.3'

description = 'OpenCADC core utility library'
def git_url = 'https://github.com/opencadc/core'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2023. (c) 2023.
* (c) 2024. (c) 2024.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
Expand Down Expand Up @@ -331,6 +331,11 @@ public static Subject getSubject(PrincipalExtractor principalExtractor, boolean
am = AuthMethod.COOKIE;
break;
}
if (o instanceof OpenIdPrincipal) {
am = AuthMethod.TOKEN;
break;
}

}
}

Expand Down
47 changes: 26 additions & 21 deletions cadc-util/src/main/java/ca/nrc/cadc/auth/OpenIdPrincipal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
************************************************************************
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
*
* (c) 2016. (c) 2016.
* (c) 2024. (c) 2024.
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits reserves
Expand Down Expand Up @@ -35,31 +35,43 @@
package ca.nrc.cadc.auth;

import java.io.Serializable;
import java.net.URL;
import java.security.Principal;

/**
* Class that represents an openID identity.
* Class that represents an openID identity. The principal consists of an immutable
* open ID and its corresponding issuer.
*/
public class OpenIdPrincipal implements Principal, Serializable {
private static final long serialVersionUID = 20140625143750L;
private static final long serialVersionUID = 202407041230L;

private String openID;
private final String sub;
private final URL issuer;

/**
* Ctor
*
* @param openID
*
* @param issuer The issuer of the Open ID
* @param sub Subject identifier.
*/
public OpenIdPrincipal(final String openID) {
if (openID == null) {
throw new IllegalArgumentException("null openID");
public OpenIdPrincipal(final URL issuer, final String sub) {
if (issuer == null) {
throw new IllegalArgumentException("null issuer");
}
if (sub == null) {
throw new IllegalArgumentException("null sub");
}
this.openID = openID;
this.sub = sub;
this.issuer = issuer;
}

@Override
public String getName() {
return openID;
return sub;
}

public URL getIssuer() {
return issuer;
}

/*
Expand All @@ -71,7 +83,7 @@ public String getName() {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((openID == null) ? 0 : openID.hashCode());
result = prime * result + sub.hashCode() + issuer.hashCode();
return result;
}

Expand All @@ -92,19 +104,12 @@ public boolean equals(Object obj) {
return false;
}
OpenIdPrincipal other = (OpenIdPrincipal) obj;
if (openID == null) {
if (other.openID != null) {
return false;
}
} else if (!openID.equals(other.openID)) {
return false;
}
return true;
return sub.equals(other.sub) && issuer.equals(other.issuer);
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + getName() + "]";
return getClass().getSimpleName() + "[issuer=" + getIssuer() + ", openID=" + getName() + "]";
}

}
129 changes: 129 additions & 0 deletions cadc-util/src/test/java/ca/nrc/cadc/auth/OpenIdPrincipalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2019. (c) 2019.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits réservés
*
* NRC disclaims any warranties, Le CNRC dénie toute garantie
* expressed, implied, or énoncée, implicite ou légale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or être tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou général,
* arising from the use of the accessoire ou fortuit, résultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote être utilisés pour approuver ou
* products derived from this promouvoir les produits dérivés
* software without specific prior de ce logiciel sans autorisation
* written permission. préalable et particulière
* par écrit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la “GNU Affero General Public
* License as published by the License” telle que publiée
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (à votre gré)
* any later version. toute version ultérieure.
*
* OpenCADC is distributed in the OpenCADC est distribué
* hope that it will be useful, dans l’espoir qu’il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans même la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ
* or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for Générale Publique GNU Affero
* more details. pour plus de détails.
*
* You should have received Vous devriez avoir reçu une
* a copy of the GNU Affero copie de la Licence Générale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n’est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* $Revision: 1 $
*
************************************************************************
*/

package ca.nrc.cadc.auth;

import ca.nrc.cadc.util.Log4jInit;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Test;
import junit.framework.Assert;

/**
*
* @author adriand
*/
public class OpenIdPrincipalTest
{
private static Logger log = Logger.getLogger(OpenIdPrincipalTest.class);

private final static URL issuer1;
private final static URL issuer2;

static {
try {
issuer1 = new URL("https://issuer1.example/openid/endpoint");
issuer2 = new URL("https://issuer2.example/openid/endpoint");
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

private final static String id1 = "abc-cdef";
private final static String id2 = "myuser";

static
{
Log4jInit.setLevel("ca.nrc.cadc.auth", Level.INFO);
Log4jInit.setLevel("ca.nrc.cadc.util", Level.INFO);
}

public OpenIdPrincipalTest()
{

}

@Test
public void testPrincipal() throws Exception
{
log.debug("test - START");
OpenIdPrincipal oip1 = new OpenIdPrincipal(issuer1, id1);
Assert.assertEquals(issuer1, oip1.getIssuer());
Assert.assertEquals(id1, oip1.getName());
Assert.assertEquals(oip1, new OpenIdPrincipal(issuer1, id1));

Assert.assertTrue(oip1 != new OpenIdPrincipal(issuer2, id1));
Assert.assertTrue(oip1 != new OpenIdPrincipal(issuer1, id2));
Assert.assertTrue(oip1 != new OpenIdPrincipal(issuer2, id2));

log.debug("testParseBearerToken - DONE");
}
}

0 comments on commit 8e7afb6

Please sign in to comment.