@@ -202,21 +202,22 @@ public void inform(final Event e) {
202202 SCANNER_OSSINDEX_API_TOKEN .getGroupName (),
203203 SCANNER_OSSINDEX_API_TOKEN .getPropertyName ()
204204 );
205- if (apiUsernameProperty == null
206- || apiUsernameProperty .getPropertyValue () == null
207- || apiTokenProperty == null
208- || apiTokenProperty .getPropertyValue () == null ) {
209- LOGGER .warn ("An API username or token has not been specified for use with OSS Index; Skipping" );
205+ if (apiTokenProperty == null || apiTokenProperty .getPropertyValue () == null ) {
206+ LOGGER .warn ("An API token has not been specified for use with OSS Index; Skipping" );
210207 return ;
211- } else {
212- try {
213- apiUsername = apiUsernameProperty .getPropertyValue ();
214- apiToken = DebugDataEncryption .decryptAsString (apiTokenProperty .getPropertyValue ());
215- } catch (Exception ex ) {
216- // OSS Index will stop supporting unauthenticated requests
217- LOGGER .error ("An error occurred decrypting the OSS Index API Token; Skipping" , ex );
208+ }
209+ try {
210+ apiToken = DebugDataEncryption .decryptAsString (apiTokenProperty .getPropertyValue ());
211+ } catch (Exception ex ) {
212+ LOGGER .error ("An error occurred decrypting the OSS Index API Token; Skipping" , ex );
213+ return ;
214+ }
215+ if (!isBearerToken (apiToken )) {
216+ if (apiUsernameProperty == null || apiUsernameProperty .getPropertyValue () == null ) {
217+ LOGGER .warn ("An API username has not been specified for use with OSS Index; Skipping" );
218218 return ;
219219 }
220+ apiUsername = apiUsernameProperty .getPropertyValue ();
220221 }
221222 aliasSyncEnabled = super .isEnabled (ConfigPropertyConstants .SCANNER_OSSINDEX_ALIAS_SYNC_ENABLED );
222223 }
@@ -322,6 +323,10 @@ private static String minimizePurl(final PackageURL purl) {
322323 return purl .getCoordinates ().replaceFirst ("@v" , "@" );
323324 }
324325
326+ private static boolean isBearerToken (final String token ) {
327+ return token != null && token .startsWith ("sonatype_pat_" );
328+ }
329+
325330 /**
326331 * Submits the payload to the Sonatype OSS Index service
327332 */
@@ -331,7 +336,9 @@ private List<ComponentReport> submit(final JSONObject payload) throws Throwable
331336 request .addHeader (HttpHeaders .CONTENT_TYPE , "application/json" );
332337 request .addHeader (HttpHeaders .USER_AGENT , ManagedHttpClientFactory .getUserAgent ());
333338 request .setEntity (new StringEntity (payload .toString ()));
334- if (apiUsername != null && apiToken != null ) {
339+ if (isBearerToken (apiToken )) {
340+ request .addHeader ("Authorization" , "Bearer " + apiToken );
341+ } else if (apiUsername != null && apiToken != null ) {
335342 request .addHeader ("Authorization" , HttpUtil .basicAuthHeaderValue (apiUsername , apiToken ));
336343 }
337344 try (final CloseableHttpResponse response = RETRY .executeCheckedSupplier (() -> HttpClientPool .getClient ().execute (request ))) {
0 commit comments