-
Notifications
You must be signed in to change notification settings - Fork 535
#6155: OAuth 2.0 - Microsoft #6192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
79ccd4c
481dc81
246d2b8
efee662
1bf38c2
ff84855
1f9630a
9ef52c5
982bd88
1638ca5
a4d8114
4828bf7
3f2420d
ed8950f
3ab5e25
97a7549
a44bb2b
31df5a2
4931d6a
44c37b4
38d88e2
ab8714e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "id":"microsoft", | ||
| "factoryAlias":"oauth2", | ||
| "title":"Microsoft", | ||
| "subtitle":"", | ||
| "factoryData":"type: microsoft | userEndpoint: NONE | clientId: FIXME | clientSecret: FIXME", | ||
| "enabled":true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,19 +127,20 @@ public OAuth2UserRecord getUserRecord(String code, @NotNull OAuth20Service servi | |
| throws IOException, OAuth2Exception, InterruptedException, ExecutionException { | ||
|
|
||
| OAuth2AccessToken accessToken = service.getAccessToken(code); | ||
|
|
||
| //final String userEndpoint = getUserEndpoint(accessToken); | ||
| // We need to check if scope is null first: GitHub is used without scope, so the responses scope is null. | ||
| // Checking scopes via Stream to be independent from order. | ||
| if ( ( accessToken.getScope() != null && ! getScope().stream().allMatch(accessToken.getScope()::contains) ) || | ||
| ( accessToken.getScope() == null && ! getSpacedScope().isEmpty() ) ) { | ||
| // We did not get the permissions on the scope(s) we need. Abort and inform the user. | ||
| throw new OAuth2Exception(200, BundleUtil.getStringFromBundle("auth.providers.insufficientScope", Arrays.asList(this.getTitle())), ""); | ||
| } | ||
|
|
||
| OAuthRequest request = new OAuthRequest(Verb.GET, getUserEndpoint(accessToken)); | ||
| request.setCharset("UTF-8"); | ||
| if (id.equals("microsoft")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still didn't get why this is needed. Could you please elaborate and add a comment next to the code so someone looking at the codebase later has a direct reminder why this is here? Thank you! |
||
| request.addHeader("Accept", "application/json"); | ||
| } | ||
| service.signRequest(accessToken, request); | ||
|
|
||
| Response response = service.execute(request); | ||
| int responseCode = response.getCode(); | ||
| String body = response.getBody(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package edu.harvard.iq.dataverse.authorization.providers.oauth2.impl; | ||
|
|
||
| import com.github.scribejava.apis.MicrosoftAzureActiveDirectory20Api; | ||
| import com.github.scribejava.core.builder.api.DefaultApi20; | ||
| import edu.harvard.iq.dataverse.authorization.providers.oauth2.AbstractOAuth2AuthenticationProvider; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.logging.Logger; | ||
| import java.io.StringReader; | ||
| import javax.json.Json; | ||
| import javax.json.JsonObject; | ||
| import javax.json.JsonReader; | ||
| import edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo; | ||
|
|
||
| /** | ||
| * | ||
| * @author | ||
| */ | ||
| public class MicrosoftOAuth2AP extends AbstractOAuth2AuthenticationProvider{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO this class should be renamed to |
||
|
|
||
| private static final Logger logger = Logger.getLogger(MicrosoftOAuth2AP.class.getCanonicalName()); | ||
|
|
||
| public MicrosoftOAuth2AP(String aClientId, String aClientSecret){ | ||
| this.id = "microsoft"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ID should correspond to the class name, so if that is going to be changed, this should change as well. |
||
| this.title = "Microsoft"; | ||
| this.clientId = aClientId; | ||
| this.clientSecret = aClientSecret; | ||
| this.scope = Arrays.asList("User.Read"); | ||
| this.baseUserEndpoint = "https://graph.microsoft.com/v1.0/me"; | ||
| } | ||
|
|
||
| @Override | ||
| public DefaultApi20 getApiInstance(){ | ||
| return MicrosoftAzureActiveDirectory20Api.instance(); | ||
| } | ||
|
|
||
| @Override | ||
| protected ParsedUserResponse parseUserResponse(final String responseBody) { | ||
| try ( StringReader rdr = new StringReader(responseBody); | ||
| JsonReader jrdr = Json.createReader(rdr) ) { | ||
| JsonObject response = jrdr.readObject(); | ||
| AuthenticatedUserDisplayInfo displayInfo = new AuthenticatedUserDisplayInfo( | ||
| response.getString("givenName", ""), | ||
| response.getString("surname", ""), | ||
| response.getString("userPrincipalName", ""), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO it's not a good idea to prefill the users email attribute with the Shouldn't we be using the Related docs:
As Dataverse is able to deal with an empty mail attribute, it should be ok if we receive an empty value.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't click any of the links above, but yes, if an email address is not provided, the user will be asked to fill it in before creating their Dataverse account.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you considered the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi! We test with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Gerafp if you're happy with the code, I'm happy. I'm moving this to QA. |
||
| "", ""); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be beyond scope, but it would be totally awesome trying to receive values for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We view the options for receive these values and can implement this in the future. It's really interesting for us. :D
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the |
||
| String persistentUserId = response.getString("id"); | ||
| String username = response.getString("userPrincipalName"); | ||
| return new ParsedUserResponse(displayInfo, persistentUserId, username, | ||
| (displayInfo.getEmailAddress().length() > 0 ? Collections.singletonList(displayInfo.getEmailAddress()) : Collections.emptyList() ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that the GitHub implementation is using this, too. I am really keen to see if this actually works, as the email response is retrieved as a string, not being converted to a list anywhere as far as I can see. I don't know if this has ever been tested with the GitHub provider. It's a good idea to test this anyway, as I don't know what happens when you receive a JSON array of mails in Could you please test this for us?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, We test this. :D |
||
| ); | ||
| } | ||
| } | ||
|
|
||
| public boolean isDisplayIdentifier() | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.