2024-01-01 14:58:21 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2023-12-14 06:05:59 -05:00
use std ::io ::IsTerminal ;
use deno_core ::anyhow ;
use deno_core ::anyhow ::bail ;
use deno_core ::error ::AnyError ;
pub enum AuthMethod {
Interactive ,
Token ( String ) ,
Oidc ( OidcConfig ) ,
}
pub struct OidcConfig {
pub url : String ,
pub token : String ,
}
2024-02-27 21:28:02 -05:00
pub ( crate ) fn is_gha ( ) -> bool {
std ::env ::var ( " GITHUB_ACTIONS " ) . unwrap_or_default ( ) = = " true "
}
pub ( crate ) fn gha_oidc_token ( ) -> Option < String > {
std ::env ::var ( " ACTIONS_ID_TOKEN_REQUEST_TOKEN " ) . ok ( )
}
2023-12-14 06:05:59 -05:00
fn get_gh_oidc_env_vars ( ) -> Option < Result < ( String , String ) , AnyError > > {
if std ::env ::var ( " GITHUB_ACTIONS " ) . unwrap_or_default ( ) = = " true " {
let url = std ::env ::var ( " ACTIONS_ID_TOKEN_REQUEST_URL " ) ;
let token = std ::env ::var ( " ACTIONS_ID_TOKEN_REQUEST_TOKEN " ) ;
match ( url , token ) {
( Ok ( url ) , Ok ( token ) ) = > Some ( Ok ( ( url , token ) ) ) ,
( Err ( _ ) , Err ( _ ) ) = > Some ( Err ( anyhow ::anyhow! (
" No means to authenticate. Pass a token to `--token`, or enable tokenless publishing from GitHub Actions using OIDC. Learn more at https://deno.co/ghoidc "
) ) ) ,
_ = > None ,
}
} else {
None
}
}
pub fn get_auth_method (
maybe_token : Option < String > ,
2024-03-06 07:56:20 -05:00
dry_run : bool ,
2023-12-14 06:05:59 -05:00
) -> Result < AuthMethod , AnyError > {
2024-03-06 07:56:20 -05:00
if dry_run {
// We don't authenticate in dry-run mode.
return Ok ( AuthMethod ::Interactive ) ;
}
2023-12-14 06:05:59 -05:00
if let Some ( token ) = maybe_token {
return Ok ( AuthMethod ::Token ( token ) ) ;
}
match get_gh_oidc_env_vars ( ) {
Some ( Ok ( ( url , token ) ) ) = > Ok ( AuthMethod ::Oidc ( OidcConfig { url , token } ) ) ,
Some ( Err ( err ) ) = > Err ( err ) ,
None if std ::io ::stdin ( ) . is_terminal ( ) = > Ok ( AuthMethod ::Interactive ) ,
None = > {
bail! ( " No means to authenticate. Pass a token to `--token`. " )
}
}
}