@@ -41,6 +41,7 @@ import {
41
41
ExternalAccountClientOptions ,
42
42
RefreshOptions ,
43
43
Impersonated ,
44
+ IdentityPoolClient ,
44
45
} from '../src' ;
45
46
import { CredentialBody } from '../src/auth/credentials' ;
46
47
import * as envDetect from '../src/auth/envDetect' ;
@@ -52,11 +53,16 @@ import {
52
53
mockStsTokenExchange ,
53
54
saEmail ,
54
55
} from './externalclienthelper' ;
55
- import { BaseExternalAccountClient } from '../src/auth/baseexternalclient' ;
56
+ import {
57
+ BaseExternalAccountClient ,
58
+ EXTERNAL_ACCOUNT_TYPE ,
59
+ } from '../src/auth/baseexternalclient' ;
56
60
import { AuthClient , DEFAULT_UNIVERSE } from '../src/auth/authclient' ;
57
61
import { ExternalAccountAuthorizedUserClient } from '../src/auth/externalAccountAuthorizedUserClient' ;
58
62
import { stringify } from 'querystring' ;
59
63
import { GoogleAuthExceptionMessages } from '../src/auth/googleauth' ;
64
+ import { IMPERSONATED_ACCOUNT_TYPE } from '../src/auth/impersonated' ;
65
+ import { USER_REFRESH_ACCOUNT_TYPE } from '../src/auth/refreshclient' ;
60
66
61
67
nock . disableNetConnect ( ) ;
62
68
@@ -1656,6 +1662,86 @@ describe('googleauth', () => {
1656
1662
. reply ( 200 , { } ) ;
1657
1663
}
1658
1664
describe ( 'for impersonated types' , ( ) => {
1665
+ describe ( 'source clients' , ( ) => {
1666
+ it ( 'should support a variety of source clients' , async ( ) => {
1667
+ const serviceAccountImpersonationURLBase =
1668
+ 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected] :generateToken' ;
1669
+ const samples : {
1670
+ creds : {
1671
+ type : typeof IMPERSONATED_ACCOUNT_TYPE ;
1672
+ service_account_impersonation_url : string ;
1673
+ source_credentials : { } ;
1674
+ } ;
1675
+ expectedSource : typeof AuthClient ;
1676
+ } [ ] = [
1677
+ // USER_TO_SERVICE_ACCOUNT_JSON
1678
+ {
1679
+ creds : {
1680
+ type : IMPERSONATED_ACCOUNT_TYPE ,
1681
+ service_account_impersonation_url : new URL (
1682
+ './[email protected] :generateAccessToken' ,
1683
+ serviceAccountImpersonationURLBase
1684
+ ) . toString ( ) ,
1685
+ source_credentials : {
1686
+ client_id : 'client' ,
1687
+ client_secret : 'secret' ,
1688
+ refresh_token : 'refreshToken' ,
1689
+ type : USER_REFRESH_ACCOUNT_TYPE ,
1690
+ } ,
1691
+ } ,
1692
+ expectedSource : UserRefreshClient ,
1693
+ } ,
1694
+ // SERVICE_ACCOUNT_TO_SERVICE_ACCOUNT_JSON
1695
+ {
1696
+ creds : {
1697
+ type : IMPERSONATED_ACCOUNT_TYPE ,
1698
+ service_account_impersonation_url : new URL (
1699
+ './[email protected] :generateIdToken' ,
1700
+ serviceAccountImpersonationURLBase
1701
+ ) . toString ( ) ,
1702
+ source_credentials : {
1703
+ type : 'service_account' ,
1704
+ client_email :
'[email protected] ' ,
1705
+ private_key : privateKey ,
1706
+ } ,
1707
+ } ,
1708
+ expectedSource : JWT ,
1709
+ } ,
1710
+ // EXTERNAL_ACCOUNT_TO_SERVICE_ACCOUNT_JSON
1711
+ {
1712
+ creds : {
1713
+ type : IMPERSONATED_ACCOUNT_TYPE ,
1714
+ service_account_impersonation_url : new URL (
1715
+ './[email protected] :generateIdToken' ,
1716
+ serviceAccountImpersonationURLBase
1717
+ ) . toString ( ) ,
1718
+ source_credentials : {
1719
+ type : EXTERNAL_ACCOUNT_TYPE ,
1720
+ audience : 'audience' ,
1721
+ subject_token_type : 'access_token' ,
1722
+ token_url : 'https://sts.googleapis.com/v1/token' ,
1723
+ credential_source : { url : 'https://example.com/token' } ,
1724
+ } ,
1725
+ } ,
1726
+ expectedSource : IdentityPoolClient ,
1727
+ } ,
1728
+ ] ;
1729
+
1730
+ const auth = new GoogleAuth ( ) ;
1731
+ for ( const { creds, expectedSource} of samples ) {
1732
+ const client = auth . fromJSON ( creds ) ;
1733
+
1734
+ assert ( client instanceof Impersonated ) ;
1735
+
1736
+ // This is a private prop - we will refactor/remove in the future
1737
+ assert (
1738
+ ( client as unknown as { sourceClient : { } } ) . sourceClient instanceof
1739
+ expectedSource
1740
+ ) ;
1741
+ }
1742
+ } ) ;
1743
+ } ) ;
1744
+
1659
1745
describe ( 'for impersonated credentials signing' , ( ) => {
1660
1746
const now = new Date ( ) . getTime ( ) ;
1661
1747
const saSuccessResponse = {
0 commit comments