11/*
2- * Copyright 2017 IBM Corp. All Rights Reserved.
2+ * Copyright 2017, 2019 IBM Corp. All Rights Reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55 * the License. You may obtain a copy of the License at
@@ -63,11 +63,9 @@ public abstract class BaseService {
6363 private static final String APIKEY_AS_USERNAME = "apikey" ;
6464 private static final String ICP_PREFIX = "icp-" ;
6565 private static final Logger LOG = Logger .getLogger (BaseService .class .getName ());
66- private String apiKey ;
6766 private String username ;
6867 private String password ;
6968 private String endPoint ;
70- private String defaultEndPoint ;
7169 private final String name ;
7270 private Authenticator authenticator ;
7371
@@ -131,8 +129,6 @@ private void setCredentialFields(CredentialUtils.ServiceCredentials serviceCrede
131129
132130 if ((serviceCredentials .getUsername () != null ) && (serviceCredentials .getPassword () != null )) {
133131 setUsernameAndPassword (serviceCredentials .getUsername (), serviceCredentials .getPassword ());
134- } else if (serviceCredentials .getOldApiKey () != null ) {
135- setApiKey (serviceCredentials .getOldApiKey ());
136132 }
137133
138134 if (serviceCredentials .getIamApiKey () != null ) {
@@ -240,18 +236,6 @@ protected final <T> ServiceCall<T> createServiceCall(final Request request, fina
240236 return new WatsonServiceCall <>(call , converter );
241237 }
242238
243- /**
244- * Gets the API key.
245- *
246- *
247- * @return the API key
248- * @deprecated
249- */
250- @ Deprecated
251- protected String getApiKey () {
252- return apiKey ;
253- }
254-
255239 /**
256240 * Gets the username.
257241 *
@@ -270,6 +254,7 @@ protected String getUsername() {
270254 *
271255 * @return the password
272256 */
257+ @ Deprecated
273258 protected String getPassword () {
274259 return password ;
275260 }
@@ -305,28 +290,13 @@ public String getName() {
305290 }
306291
307292 /**
308- * Sets the API key.
293+ * Sets the authentication information on the specified request builder.
294+ * This method will invoke the configured Authenticator instance to perform the
295+ * authentication needed for that authentication type. This could involve adding an
296+ * Authorization header, or perhaps adding a specific query param to the request URL.
309297 *
310- * @param apiKey the new API key
311- * @deprecated Use setAuthenticator(AuthenticatorConfig) instead
312- */
313- @ Deprecated
314- public void setApiKey (String apiKey ) {
315- if (CredentialUtils .hasBadStartOrEndChar (apiKey )) {
316- throw new IllegalArgumentException ("The API key shouldn't start or end with curly brackets or quotes. Please "
317- + "remove any surrounding {, }, or \" characters." );
318- }
319-
320- if (this .endPoint .equals (this .defaultEndPoint )) {
321- this .endPoint = "https://gateway-a.watsonplatform.net/visual-recognition/api" ;
322- }
323- this .apiKey = apiKey ;
324- }
325-
326- /**
327- * Sets the authentication. Okhttp3 compliant.
328- *
329- * @param builder the new authentication
298+ * @param builder the request builder that represents the outgoing requst on which
299+ * the authentication information should be set.
330300 */
331301 protected void setAuthentication (final Builder builder ) {
332302 if (this .skipAuthentication ) {
@@ -353,15 +323,12 @@ public void setEndPoint(final String endPoint) {
353323
354324 if ((endPoint != null ) && !endPoint .isEmpty ()) {
355325 String newEndPoint = endPoint .endsWith ("/" ) ? endPoint .substring (0 , endPoint .length () - 1 ) : endPoint ;
356- if (this .endPoint == null ) {
357- this .defaultEndPoint = newEndPoint ;
358- }
359326 this .endPoint = newEndPoint ;
360327 }
361328 }
362329
363330 /**
364- * Sets the username and password.
331+ * Sets the username and password on this instance .
365332 *
366333 * @param username the username
367334 * @param password the password
@@ -374,13 +341,19 @@ public void setUsernameAndPassword(final String username, final String password)
374341 + "quotes. Please remove any surrounding {, }, or \" characters." );
375342 }
376343
377- // we'll perform the token exchange for users UNLESS they're on ICP
344+ // These fields are set to provide compatibility with the getUsername() and getPassword() methods.
345+ this .username = username ;
346+ this .password = password ;
347+
348+ // If this method is being called to set the IAM api key, we'll configure the IAM authenticator.
349+ // Note that we only do this if the password does NOT indicate ICP.
378350 if (username .equals (APIKEY_AS_USERNAME ) && !password .startsWith (ICP_PREFIX )) {
379351 IamOptions iamOptions = new IamOptions .Builder ()
380352 .apiKey (password )
381353 .build ();
382354 setAuthenticator (iamOptions );
383355 } else {
356+ // Otherwise, we'll just use the username and password to configure basic auth.
384357 BasicAuthConfig basicAuthConfig = new BasicAuthConfig .Builder ()
385358 .username (username )
386359 .password (password )
@@ -418,15 +391,19 @@ public void setIamCredentials(IamOptions iamOptions) {
418391 }
419392
420393 /**
421- * Initializes a new Authenticator instance based on the input AuthenticatorConfig instance and sets it as
422- * the current authenticator on the BaseService instance.
394+ * Initializes a new Authenticator instance based on the input AuthenticatorConfig instance,
395+ * and sets it as the current authenticator on this BaseService instance.
396+ * The "skipAuthentication" flag is updated appropriately by this method.
423397 * @param authConfig the AuthenticatorConfig instance containing the authentication configuration
424398 */
425- protected void setAuthenticator (AuthenticatorConfig authConfig ) {
399+ public void setAuthenticator (AuthenticatorConfig authConfig ) {
426400 try {
427- this .authenticator = AuthenticatorFactory .getAuthenticator (authConfig );
428- if (authenticator instanceof NoauthAuthenticator ) {
429- setSkipAuthentication (true );
401+ if (authConfig == null ) {
402+ this .authenticator = null ;
403+ this .skipAuthentication = false ;
404+ } else {
405+ this .authenticator = AuthenticatorFactory .getAuthenticator (authConfig );
406+ this .skipAuthentication = (Authenticator .AUTHTYPE_NOAUTH .equals (authConfig .authenticationType ()));
430407 }
431408 } catch (Exception e ) {
432409 throw new RuntimeException (e );
@@ -503,14 +480,30 @@ protected <T> T processServiceCall(final ResponseConverter<T> converter, final R
503480 * Sets the skip authentication.
504481 *
505482 * @param skipAuthentication the new skip authentication
483+ *
484+ * @deprecated Use setAuthenticator(new NoauthConfig()) instead
506485 */
486+ @ Deprecated
507487 public void setSkipAuthentication (final boolean skipAuthentication ) {
508488 this .skipAuthentication = skipAuthentication ;
509489 if (this .skipAuthentication ) {
510490 this .authenticator = new NoauthAuthenticator ((NoauthConfig ) null );
491+ } else {
492+ // If we're setting skipAuthentication to false, make sure we
493+ // clear out the authenticator if it's currently set to "noauth".
494+ if (Authenticator .AUTHTYPE_NOAUTH .equals (this .authenticator .authenticationType ())) {
495+ this .authenticator = null ;
496+ }
511497 }
512498 }
513499
500+ /**
501+ * Returns true iff this instance has been configured to bypass authentication for outgoing requests.
502+ * @return true to indicate authentication is being bypassed, false otherwise.
503+ *
504+ * @deprecated
505+ */
506+ @ Deprecated
514507 public boolean isSkipAuthentication () {
515508 return this .skipAuthentication ;
516509 }
0 commit comments