11/**
22 * Shared IAM auth helpers
3- * Consolidates OAuth check + auth check + config building patterns
3+ * Uses resolveAuthMethod() as the single source of truth for auth priority.
44 */
55
66import { failWithError } from '@utils/exit.js' ;
77import type { MessageContext } from '@utils/messages.js' ;
88
99import { getAuthClient } from './client.js' ;
10- import { isFlyUser } from './fly.js' ;
11- import { getCredentials , getLoginMethod , getTigrisConfig } from './provider.js' ;
12- import { getSelectedOrganization } from './storage.js' ;
13-
14- /**
15- * Check if current org is Fly.io. Prints message and returns true if so.
16- */
17- export function isFlyOrganization ( ) : boolean {
18- const selectedOrg = getSelectedOrganization ( ) ;
19- if ( isFlyUser ( selectedOrg ?? undefined ) ) {
20- console . log (
21- 'User management is not available for Fly.io organizations.\n' +
22- 'Your users are managed through Fly.io.\n\n' +
23- 'Visit https://fly.io to manage your organization members.'
24- ) ;
25- return true ;
26- }
27- return false ;
28- }
10+ export { isFlyOrganization } from './fly.js' ;
11+ import { getTigrisConfig , resolveAuthMethod } from './provider.js' ;
12+ import { getLoginMethod , getSelectedOrganization } from './storage.js' ;
2913
3014/**
3115 * OAuth-only IAM config. Exits on non-OAuth or unauthenticated.
3216 * Used by IAM policy and user commands.
17+ *
18+ * Checks the *stored* login method (not resolveAuthMethod) because these
19+ * operations always require OAuth — even when env vars or AWS profile
20+ * are set for S3.
3321 */
3422export async function getOAuthIAMConfig ( context : MessageContext ) {
35- const loginMethod = await getLoginMethod ( ) ;
36- if ( loginMethod !== 'oauth' ) {
23+ if ( getLoginMethod ( ) !== 'oauth' ) {
3724 failWithError (
3825 context ,
3926 'This operation requires OAuth login.\nRun "tigris login oauth" first.'
@@ -48,12 +35,11 @@ export async function getOAuthIAMConfig(context: MessageContext) {
4835 ) ;
4936 }
5037
51- const accessToken = await authClient . getAccessToken ( ) ;
5238 const selectedOrg = getSelectedOrganization ( ) ;
5339 const { iamEndpoint, mgmtEndpoint } = getTigrisConfig ( ) ;
5440
5541 return {
56- sessionToken : accessToken ,
42+ sessionToken : await authClient . getAccessToken ( ) ,
5743 organizationId : selectedOrg ?? undefined ,
5844 iamEndpoint,
5945 mgmtEndpoint,
@@ -62,41 +48,31 @@ export async function getOAuthIAMConfig(context: MessageContext) {
6248
6349/**
6450 * Dual-mode IAM config (OAuth or credentials).
51+ * Uses resolveAuthMethod() to follow the same priority as getStorageConfig().
6552 * Used by access-key commands.
6653 */
6754export async function getIAMConfig ( context : MessageContext ) {
68- const loginMethod = await getLoginMethod ( ) ;
69- const tigrisConfig = getTigrisConfig ( ) ;
70- const selectedOrg = getSelectedOrganization ( ) ;
55+ const method = await resolveAuthMethod ( ) ;
56+
57+ switch ( method . type ) {
58+ case 'oauth' :
59+ return getOAuthIAMConfig ( context ) ;
60+
61+ case 'aws-profile' :
62+ case 'credentials' :
63+ case 'environment' :
64+ case 'configured' :
65+ return {
66+ accessKeyId : method . accessKeyId ,
67+ secretAccessKey : method . secretAccessKey ,
68+ organizationId : getSelectedOrganization ( ) ?? undefined ,
69+ iamEndpoint : getTigrisConfig ( ) . iamEndpoint ,
70+ } ;
7171
72- if ( loginMethod === 'oauth' ) {
73- const authClient = getAuthClient ( ) ;
74- if ( ! ( await authClient . isAuthenticated ( ) ) ) {
72+ case 'none' :
7573 failWithError (
7674 context ,
77- 'Not authenticated. Run "tigris login oauth " first.'
75+ 'Not authenticated. Run "tigris login" or "tigris configure " first.'
7876 ) ;
79- }
80-
81- return {
82- sessionToken : await authClient . getAccessToken ( ) ,
83- organizationId : selectedOrg ?? undefined ,
84- iamEndpoint : tigrisConfig . iamEndpoint ,
85- } ;
86- }
87-
88- const credentials = getCredentials ( ) ;
89- if ( ! credentials ) {
90- failWithError (
91- context ,
92- 'Not authenticated. Run "tigris login" or "tigris configure" first.'
93- ) ;
9477 }
95-
96- return {
97- accessKeyId : credentials . accessKeyId ,
98- secretAccessKey : credentials . secretAccessKey ,
99- organizationId : selectedOrg ?? undefined ,
100- iamEndpoint : tigrisConfig . iamEndpoint ,
101- } ;
10278}
0 commit comments