13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package dev .sigstore .cli ;
16
+ package dev .sigstore .oidc . client ;
17
17
18
18
import com .google .api .client .json .gson .GsonFactory ;
19
19
import com .google .api .client .json .webtoken .JsonWebSignature ;
20
- import dev .sigstore .oidc .client .ImmutableOidcToken ;
21
- import dev .sigstore .oidc .client .OidcClient ;
22
- import dev .sigstore .oidc .client .OidcException ;
23
- import dev .sigstore .oidc .client .OidcToken ;
24
20
import java .io .IOException ;
25
21
import java .util .Map ;
26
22
23
+ /**
24
+ * This should only be used when the user has an out of band mechanism for obtaining an OIDC token
25
+ * to be consumed by a sigstore signing event. So it should not be included in any defaults for
26
+ * {@link OidcClients}.
27
+ *
28
+ * <p>It's not explicitly designed for multi use, but implementers of the {@link
29
+ * TokenStringProvider} may include mechanisms for longer lived signing events. Each time a token is
30
+ * requested, the provider may execute a fetch of the token.
31
+ */
27
32
public class TokenStringOidcClient implements OidcClient {
28
33
29
- private final String idToken ;
34
+ private final TokenStringProvider idTokenProvider ;
35
+
36
+ TokenStringOidcClient (TokenStringProvider provider ) {
37
+ this .idTokenProvider = provider ;
38
+ }
39
+
40
+ public static TokenStringOidcClient from (TokenStringProvider provider ) {
41
+ return new TokenStringOidcClient (provider );
42
+ }
30
43
31
- public TokenStringOidcClient (String idToken ) {
32
- this . idToken = idToken ;
44
+ public static TokenStringOidcClient from (String token ) {
45
+ return new TokenStringOidcClient (() -> token ) ;
33
46
}
34
47
35
48
@ Override
@@ -40,6 +53,7 @@ public boolean isEnabled(Map<String, String> env) {
40
53
@ Override
41
54
public OidcToken getIDToken (Map <String , String > env ) throws OidcException {
42
55
try {
56
+ var idToken = idTokenProvider .getTokenString ();
43
57
var jws = JsonWebSignature .parse (new GsonFactory (), idToken );
44
58
return ImmutableOidcToken .builder ()
45
59
.idToken (idToken )
@@ -48,6 +62,13 @@ public OidcToken getIDToken(Map<String, String> env) throws OidcException {
48
62
.build ();
49
63
} catch (IOException e ) {
50
64
throw new OidcException ("Failed to parse JWT" , e );
65
+ } catch (Exception e ) {
66
+ throw new OidcException ("Failed to obtain token" , e );
51
67
}
52
68
}
69
+
70
+ @ FunctionalInterface
71
+ public interface TokenStringProvider {
72
+ String getTokenString () throws Exception ;
73
+ }
53
74
}
0 commit comments