package org.eurekaclinical.scribeupext.provider;

/*
 * #%L
 * Eureka! Clinical ScribeUP Extensions
 * %%
 * Copyright (C) 2014 Emory University
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
import com.fasterxml.jackson.databind.JsonNode;
import org.eurekaclinical.scribeupext.GlobusApi;
import org.eurekaclinical.scribeupext.profile.EurekaAttributesDefinition;
import org.eurekaclinical.scribeupext.profile.GlobusAttributesDefinition;
import org.eurekaclinical.scribeupext.profile.GlobusProfile;
import org.eurekaclinical.scribeupext.service.GlobusOAuth20ServiceImpl;
import java.util.concurrent.TimeUnit;
import org.scribe.model.OAuthConfig;
import org.scribe.model.ProxyOAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.SignatureType;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.up.profile.JsonHelper;
import org.scribe.up.profile.UserProfile;
import org.scribe.up.provider.BaseOAuth20Provider;
import org.scribe.up.provider.exception.HttpException;

/**
 * Extracts the user's Globus profile.
 * 
 * @author Andrew Post
 */
public class GlobusProvider extends BaseOAuth20Provider {

    @Override
    protected GlobusProvider newProvider() {
        return new GlobusProvider();
    }

    @Override
    protected void internalInit() {
        this.service = new GlobusOAuth20ServiceImpl(new GlobusApi(),
                new OAuthConfig(this.key, this.secret, this.callbackUrl,
                        SignatureType.Header, "user", null), this.proxyHost,
                this.proxyPort);
    }

    /**
     * Returns the current URL for requesting the user's profile.
     * 
     * @return the URL for requesting the user's profile.
     */
    @Override
    protected String getProfileUrl() {
        return "https://auth.globus.org/v2/oauth2/userinfo";
    }

    @Override
    protected String sendRequestForData(Token accessToken, String dataUrl) throws HttpException {
        final ProxyOAuthRequest request = new ProxyOAuthRequest(Verb.GET, dataUrl, this.proxyHost, this.proxyPort);
        if (this.connectTimeout != 0) {
            request.setConnectTimeout(this.connectTimeout, TimeUnit.MILLISECONDS);
        }
        if (this.readTimeout != 0) {
            request.setReadTimeout(this.readTimeout, TimeUnit.MILLISECONDS);
        }
        this.service.signRequest(accessToken, request);
        request.addHeader("Content-Type", "application/json");
        final Response response = request.send();
        final int code = response.getCode();
        final String body = response.getBody();
        if (code != 200) {
            throw new HttpException(code, body);
        }
        return body;
    }

    /**
     * Returns the user's profile using the attributes that Eureka! Clinical 
     * expects.
     * 
     * @param body the JSON response from the user profile request.
     * 
     * @return the user's profile.
     */
    @Override
    protected UserProfile extractUserProfile(final String body) {
        GlobusProfile profile = new GlobusProfile();
        JsonNode json = JsonHelper.getFirstNode(body);
        if (json != null) {
            String username = JsonHelper.get(json, GlobusAttributesDefinition.USERNAME).toString().split("@")[0];
            profile.setId(username);
            profile.addAttribute(EurekaAttributesDefinition.USERNAME, username);
            profile.addAttribute(EurekaAttributesDefinition.FULLNAME, JsonHelper.get(json, GlobusAttributesDefinition.FULLNAME));
            profile.addAttribute(EurekaAttributesDefinition.EMAIL, JsonHelper.get(json, GlobusAttributesDefinition.EMAIL));
        }
        return profile;
    }
}

@@ -1575,24 +1575,25 @@
 profile.
+ 
 %0A * %0A * 
 @author 
@@ -1588,89 +1588,758 @@
 %0A * 
-@author Andrew Post%0A */%0Apublic class GlobusProvider extends BaseOAuth20Provider %7B
+Globus changed their usernames in August, 2017 by appending %0A * %3Ccode%3E@globusid.org%3C/code%3E to them. This is true even for user accounts that%0A * predate the change. To give some time for users of this library to move to %0A * the new username scheme, we provide a system property, %0A * %3Ccode%3Escribeupext.globus.legacyUsernames%3C/code%3E, which if %3Ccode%3Etrue%3C/code%3E, %0A * will cause this class to remove the %3Ccode%3E@globusid.org%3C/code%3E suffix from %0A * usernames when extracting a user's profile. This system property may be%0A * removed in the next major release.%0A *%0A * @author Andrew Post%0A */%0Apublic class GlobusProvider extends BaseOAuth20Provider %7B%0A%0A    private static final boolean LEGACY_USERNAMES = Boolean.getBoolean(%22scribeupext.globus.legacyUsernames%22);
 %0A%0A  
@@ -2820,33 +2820,32 @@
  profile.%0A     *
- 
 %0A     * @return 
@@ -3961,17 +3961,16 @@
 Clinical
- 
 %0A     * 
@@ -3977,24 +3977,272 @@
 expects.
+ If the system property %0A     * %3Ccode%3Escribeupext.globus.legacyUsernames%3C/code%3E is set and has value%0A     * %3Ccode%3Etrue%3C/code%3E (case-insensitive), this method will omit the%0A     * %3Ccode%3E@globusid.org%3C/code%3E suffix when extracting the user's username.
 %0A     *
- 
 %0A     * 
@@ -4308,17 +4308,16 @@
 .%0A     *
- 
 %0A     * 
@@ -4666,16 +4666,89 @@
 String()
+;%0A            if (LEGACY_USERNAMES) %7B%0A                username = username
 .split(%22
@@ -4755,16 +4755,30 @@
 @%22)%5B0%5D;%0A
+            %7D%0A
         

Add a system property to make the Globus OAuth support use the old username behavior.