3131import java .net .URISyntaxException ;
3232import java .util .List ;
3333import java .util .Optional ;
34+ import java .util .regex .Pattern ;
3435
3536public interface AuthTokenProvider {
3637
@@ -70,7 +71,8 @@ default URI addUserInfoToUri(URI repo, @Nullable Secret secret) {
7071
7172 @ SuppressWarnings ("ClassCanBeRecord" )
7273 class OauthTokenProvider implements AuthTokenProvider {
73-
74+ // >0 length, printable ascii (no newlines, etc)
75+ private static final Pattern BASIC_STRING_PTN = Pattern .compile ("[ -~]+" );
7476 private final List <MappingAuthConfig > authConfigs ;
7577
7678 @ Inject
@@ -85,6 +87,16 @@ public boolean supports(URI repo, @Nullable Secret secret) {
8587
8688 @ Override
8789 public Optional <ExternalAuthToken > getToken (URI repo , @ Nullable Secret secret ) {
90+ if (secret != null ) {
91+ if (secret instanceof BinaryDataSecret bds ) {
92+ return Optional .of (ExternalAuthToken .StaticToken .builder ()
93+ .token (new String (bds .getData ()))
94+ .build ());
95+ } else {
96+ return Optional .empty ();
97+ }
98+ }
99+
88100 return authConfigs .stream ()
89101 .filter (auth -> auth .canHandle (repo ))
90102 .filter (MappingAuthConfig .OauthAuthConfig .class ::isInstance )
@@ -105,9 +117,8 @@ private boolean validateSecret(Secret secret) {
105117 // this class is not the place for handling key pairs or username/password
106118 return false ;
107119 } else {
108- String data = new String (bds .getData ());
109- // >0 length, printable ascii (no newlines, etc)
110- return data .matches ("[ -~]+" );
120+ var data = new String (bds .getData ());
121+ return BASIC_STRING_PTN .matcher (data ).matches ();
111122 }
112123 }
113124
0 commit comments