forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge dataporten into 4334-oauth-dataporten
- Loading branch information
Showing
7 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,5 +236,6 @@ public enum DevOAuthAccountType { | |
RANDOM_EMAIL1, | ||
RANDOM_EMAIL2, | ||
RANDOM_EMAIL3, | ||
RANDOM_EMAIL4, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...main/java/edu/harvard/iq/dataverse/authorization/providers/oauth2/impl/DataportenApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package edu.harvard.iq.dataverse.authorization.providers.oauth2.impl; | ||
|
||
import com.github.scribejava.core.builder.api.DefaultApi20; | ||
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor; | ||
import com.github.scribejava.core.extractors.TokenExtractor; | ||
import com.github.scribejava.core.model.OAuth2AccessToken; | ||
import com.github.scribejava.core.model.Verb; | ||
|
||
/** | ||
* | ||
* @author ran033@uit.no (Ruben Andreassen) | ||
*/ | ||
public class DataportenApi extends DefaultApi20 { | ||
|
||
protected DataportenApi() { | ||
} | ||
|
||
private static class InstanceHolder { | ||
private static final DataportenApi INSTANCE = new DataportenApi(); | ||
} | ||
|
||
public static DataportenApi instance() { | ||
return InstanceHolder.INSTANCE; | ||
} | ||
|
||
@Override | ||
public Verb getAccessTokenVerb() { | ||
return Verb.POST; | ||
} | ||
|
||
@Override | ||
public String getAccessTokenEndpoint() { | ||
return "https://auth.dataporten.no/oauth/token"; | ||
} | ||
|
||
@Override | ||
protected String getAuthorizationBaseUrl() { | ||
return "https://auth.dataporten.no/oauth/authorization"; | ||
} | ||
|
||
@Override | ||
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() { | ||
return OAuth2AccessTokenJsonExtractor.instance(); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...java/edu/harvard/iq/dataverse/authorization/providers/oauth2/impl/DataportenOAuth2AP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package edu.harvard.iq.dataverse.authorization.providers.oauth2.impl; | ||
|
||
import com.github.scribejava.core.builder.api.BaseApi; | ||
import edu.emory.mathcs.backport.java.util.Collections; | ||
import edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo; | ||
import edu.harvard.iq.dataverse.authorization.providers.oauth2.AbstractOAuth2AuthenticationProvider; | ||
import edu.harvard.iq.dataverse.authorization.providers.shib.ShibUserNameFields; | ||
import edu.harvard.iq.dataverse.authorization.providers.shib.ShibUtil; | ||
import edu.harvard.iq.dataverse.util.BundleUtil; | ||
import java.io.StringReader; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonReader; | ||
import javax.json.JsonArray; | ||
|
||
/** | ||
* | ||
* @author ran033@uit.no (Ruben Andreassen) | ||
*/ | ||
public class DataportenOAuth2AP extends AbstractOAuth2AuthenticationProvider { | ||
|
||
public DataportenOAuth2AP(String aClientId, String aClientSecret) { | ||
id = "dataporten"; | ||
title = BundleUtil.getStringFromBundle("auth.providers.title.dataporten"); | ||
clientId = aClientId; | ||
clientSecret = aClientSecret; | ||
baseUserEndpoint = "https://auth.dataporten.no/userinfo"; | ||
} | ||
|
||
@Override | ||
public BaseApi getApiInstance() { | ||
return DataportenApi.instance(); | ||
} | ||
|
||
@Override | ||
protected ParsedUserResponse parseUserResponse( String responseBody ) { | ||
|
||
try ( StringReader rdr = new StringReader(responseBody); | ||
JsonReader jrdr = Json.createReader(rdr) ) { | ||
JsonObject responseObject = jrdr.readObject(); | ||
JsonObject userObject = responseObject.getJsonObject("user"); | ||
JsonArray userid_secArray = userObject.getJsonArray("userid_sec"); | ||
|
||
String username = ""; | ||
|
||
/* | ||
Example reponse | ||
{ | ||
"user": { | ||
"userid": "76a7a061-3c55-430d-8ee0-6f82ec42501f", | ||
"userid_sec": ["feide:andreas@uninett.no"], | ||
"name": "Andreas \u00c5kre Solberg", | ||
"email": "andreas.solberg@uninett.no", | ||
"profilephoto": "p:a3019954-902f-45a3-b4ee-bca7b48ab507" | ||
}, | ||
"audience": "e8160a77-58f8-4006-8ee5-ab64d17a5b1e" | ||
} | ||
*/ | ||
|
||
// Extract ad username using regexp | ||
Pattern p = Pattern.compile("^feide:([0-9a-zA-Z]+?)@.*$"); | ||
Matcher m = p.matcher(userid_secArray.getString(0)); | ||
if(m.matches()) { | ||
username = m.group(1); | ||
} | ||
|
||
ShibUserNameFields shibUserNameFields = ShibUtil.findBestFirstAndLastName(null, null, userObject.getString("name","")); | ||
AuthenticatedUserDisplayInfo displayInfo = new AuthenticatedUserDisplayInfo( | ||
shibUserNameFields.getFirstName(), | ||
shibUserNameFields.getLastName(), | ||
userObject.getString("email",""), | ||
"", //company | ||
"" | ||
); | ||
|
||
return new ParsedUserResponse( | ||
displayInfo, | ||
userObject.getString("userid"), //persistentUserId | ||
userObject.getString("email"), //username | ||
displayInfo.getEmailAddress().length()>0 ? Collections.singletonList(displayInfo.getEmailAddress()) | ||
: Collections.emptyList() ); | ||
|
||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean isDisplayIdentifier() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getPersistentIdName() { | ||
return BundleUtil.getStringFromBundle("auth.providers.persistentUserIdName.dataporten"); | ||
} | ||
|
||
@Override | ||
public String getPersistentIdDescription() { | ||
return BundleUtil.getStringFromBundle("auth.providers.persistentUserIdTooltip.dataporten"); | ||
} | ||
|
||
@Override | ||
public String getPersistentIdUrlPrefix() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getLogo() { | ||
return null; | ||
} | ||
} |