@@ -25,6 +25,31 @@ import (
2525 "github.com/authorizerdev/authorizer/internal/token"
2626)
2727
28+ // Default values for flags (single source of truth for init and applyFlagDefaults).
29+ var (
30+ defaultHost = "0.0.0.0"
31+ defaultLogLevel = "debug"
32+ defaultHTTPPort = 8080
33+ defaultMetricsPort = 8081
34+ defaultOrganizationLogo = "https://authorizer.dev/images/logo.png"
35+ defaultOrganizationName = "Authorizer"
36+ defaultAdminSecret = "password"
37+ defaultJWTRoleClaim = "role"
38+ defaultMicrosoftTenantID = "common"
39+ defaultAllowedOrigins = []string {"*" }
40+ defaultRoles = []string {"user" }
41+ defaultGoogleScopes = []string {"openid" , "profile" , "email" }
42+ defaultGithubScopes = []string {"read:user" , "user:email" }
43+ defaultFacebookScopes = []string {"public_profile" , "email" }
44+ defaultMicrosoftScopes = []string {"openid" , "profile" , "email" }
45+ defaultTwitchScopes = []string {"openid" , "user:read:email" }
46+ defaultLinkedinScopes = []string {"r_liteprofile" , "r_emailaddress" }
47+ defaultAppleScopes = []string {"email" , "name" }
48+ defaultDiscordScopes = []string {"identify" , "email" }
49+ defaultTwitterScopes = []string {"tweet.read" , "users.read" }
50+ defaultRobloxScopes = []string {"openid" , "profile" }
51+ )
52+
2853var (
2954 RootCmd = cobra.Command {
3055 Use : "authorizer" ,
@@ -41,12 +66,12 @@ func init() {
4166 f := RootCmd .Flags ()
4267
4368 // Server flags
44- f .StringVar (& rootArgs .server .Host , "host" , "0.0.0.0" , "Host address to listen on" )
45- f .IntVar (& rootArgs .server .HTTPPort , "http-port" , 8080 , "Port to serve HTTP requests on" )
46- f .IntVar (& rootArgs .server .MetricsPort , "metrics-port" , 8081 , "Port to serve metrics requests on" )
69+ f .StringVar (& rootArgs .server .Host , "host" , defaultHost , "Host address to listen on" )
70+ f .IntVar (& rootArgs .server .HTTPPort , "http-port" , defaultHTTPPort , "Port to serve HTTP requests on" )
71+ f .IntVar (& rootArgs .server .MetricsPort , "metrics-port" , defaultMetricsPort , "Port to serve metrics requests on" )
4772
4873 // Logging flags
49- f .StringVar (& rootArgs .logLevel , "log-level" , "debug" , "Log level to use" )
74+ f .StringVar (& rootArgs .logLevel , "log-level" , defaultLogLevel , "Log level to use" )
5075
5176 // Env
5277 f .StringVar (& rootArgs .config .Env , "env" , "" , "Environment of the authorizer instance" )
@@ -57,8 +82,8 @@ func init() {
5782 f .BoolVar (& rootArgs .config .EnableGraphQLIntrospection , "enable-graphql-introspection" , true , "Enable GraphQL introspection for the /graphql endpoint" )
5883
5984 // Organization flags
60- f .StringVar (& rootArgs .config .OrganizationLogo , "organization-logo" , "https://authorizer.dev/images/logo.png" , "Logo of the organization" )
61- f .StringVar (& rootArgs .config .OrganizationName , "organization-name" , "Authorizer" , "Name of the organization" )
85+ f .StringVar (& rootArgs .config .OrganizationLogo , "organization-logo" , defaultOrganizationLogo , "Logo of the organization" )
86+ f .StringVar (& rootArgs .config .OrganizationName , "organization-name" , defaultOrganizationName , "Name of the organization" )
6287
6388 // OAuth flags
6489 f .StringVar (& rootArgs .config .ClientID , "client-id" , "" , "Client ID for the OAuth" )
@@ -67,10 +92,10 @@ func init() {
6792 f .StringVar (& rootArgs .config .DefaultAuthorizeResponseType , "default-authorize-response-type" , constants .ResponseTypeToken , "Default response type for the authorize endpoint" )
6893
6994 // Admin flags
70- f .StringVar (& rootArgs .config .AdminSecret , "admin-secret" , "password" , "Secret for the admin" )
95+ f .StringVar (& rootArgs .config .AdminSecret , "admin-secret" , defaultAdminSecret , "Secret for the admin" )
7196
7297 // Allowed origins
73- f .StringSliceVar (& rootArgs .config .AllowedOrigins , "allowed-origins" , [] string { "*" } , "Allowed origins" )
98+ f .StringSliceVar (& rootArgs .config .AllowedOrigins , "allowed-origins" , defaultAllowedOrigins , "Allowed origins" )
7499
75100 // Database flags
76101 f .StringVar (& rootArgs .config .DatabaseType , "database-type" , "" , "Type of database to use" )
@@ -104,8 +129,8 @@ func init() {
104129 f .BoolVar (& rootArgs .config .SMTPSkipTLSVerification , "smtp-skip-tls-verification" , false , "Skip TLS verification for the SMTP server" )
105130
106131 // Auth flags
107- f .StringSliceVar (& rootArgs .config .DefaultRoles , "default-roles" , [] string { "user" } , "Default user roles to assign" )
108- f .StringSliceVar (& rootArgs .config .Roles , "roles" , [] string { "user" } , "Roles to assign" )
132+ f .StringSliceVar (& rootArgs .config .DefaultRoles , "default-roles" , defaultRoles , "Default user roles to assign" )
133+ f .StringSliceVar (& rootArgs .config .Roles , "roles" , defaultRoles , "Roles to assign" )
109134 f .StringSliceVar (& rootArgs .config .ProtectedRoles , "protected-roles" , []string {}, "Roles that cannot be deleted" )
110135 f .BoolVar (& rootArgs .config .EnableStrongPassword , "enable-strong-password" , true , "Enable strong password requirement" )
111136 f .BoolVar (& rootArgs .config .EnableTOTPLogin , "enable-totp-login" , false , "Enable TOTP login" )
@@ -130,7 +155,7 @@ func init() {
130155 f .StringVar (& rootArgs .config .JWTSecret , "jwt-secret" , "" , "Secret for the JWT" )
131156 f .StringVar (& rootArgs .config .JWTPrivateKey , "jwt-private-key" , "" , "Private key for the JWT" )
132157 f .StringVar (& rootArgs .config .JWTPublicKey , "jwt-public-key" , "" , "Public key for the JWT" )
133- f .StringVar (& rootArgs .config .JWTRoleClaim , "jwt-role-claim" , "role" , "Role claim for the JWT" )
158+ f .StringVar (& rootArgs .config .JWTRoleClaim , "jwt-role-claim" , defaultJWTRoleClaim , "Role claim for the JWT" )
134159 f .StringVar (& rootArgs .config .CustomAccessTokenScript , "custom-access-token-script" , "" , "Custom access token script" )
135160
136161 // Twilio flags
@@ -142,35 +167,35 @@ func init() {
142167 // Oauth provider flags
143168 f .StringVar (& rootArgs .config .GoogleClientID , "google-client-id" , "" , "Client ID for Google" )
144169 f .StringVar (& rootArgs .config .GoogleClientSecret , "google-client-secret" , "" , "Client secret for Google" )
145- f .StringSliceVar (& rootArgs .config .GoogleScopes , "google-scopes" , [] string { "openid" , "profile" , "email" } , "Scopes for Google" )
170+ f .StringSliceVar (& rootArgs .config .GoogleScopes , "google-scopes" , defaultGoogleScopes , "Scopes for Google" )
146171 f .StringVar (& rootArgs .config .GithubClientID , "github-client-id" , "" , "Client ID for Github" )
147172 f .StringVar (& rootArgs .config .GithubClientSecret , "github-client-secret" , "" , "Client secret for Github" )
148- f .StringSliceVar (& rootArgs .config .GithubScopes , "github-scopes" , [] string { "read:user" , "user:email" } , "Scopes for Github" )
173+ f .StringSliceVar (& rootArgs .config .GithubScopes , "github-scopes" , defaultGithubScopes , "Scopes for Github" )
149174 f .StringVar (& rootArgs .config .FacebookClientID , "facebook-client-id" , "" , "Client ID for Facebook" )
150175 f .StringVar (& rootArgs .config .FacebookClientSecret , "facebook-client-secret" , "" , "Client secret for Facebook" )
151- f .StringSliceVar (& rootArgs .config .FacebookScopes , "facebook-scopes" , [] string { "public_profile" , "email" } , "Scopes for Facebook" )
176+ f .StringSliceVar (& rootArgs .config .FacebookScopes , "facebook-scopes" , defaultFacebookScopes , "Scopes for Facebook" )
152177 f .StringVar (& rootArgs .config .MicrosoftClientID , "microsoft-client-id" , "" , "Client ID for Microsoft" )
153178 f .StringVar (& rootArgs .config .MicrosoftClientSecret , "microsoft-client-secret" , "" , "Client secret for Microsoft" )
154- f .StringVar (& rootArgs .config .MicrosoftTenantID , "microsoft-tenant-id" , "common" , "Tenant ID for Microsoft" )
155- f .StringSliceVar (& rootArgs .config .MicrosoftScopes , "microsoft-scopes" , [] string { "openid" , "profile" , "email" } , "Scopes for Microsoft" )
179+ f .StringVar (& rootArgs .config .MicrosoftTenantID , "microsoft-tenant-id" , defaultMicrosoftTenantID , "Tenant ID for Microsoft" )
180+ f .StringSliceVar (& rootArgs .config .MicrosoftScopes , "microsoft-scopes" , defaultMicrosoftScopes , "Scopes for Microsoft" )
156181 f .StringVar (& rootArgs .config .TwitchClientID , "twitch-client-id" , "" , "Client ID for Twitch" )
157182 f .StringVar (& rootArgs .config .TwitchClientSecret , "twitch-client-secret" , "" , "Client secret for Twitch" )
158- f .StringSliceVar (& rootArgs .config .TwitchScopes , "twitch-scopes" , [] string { "openid" , "user:read:email" } , "Scopes for Twitch" )
183+ f .StringSliceVar (& rootArgs .config .TwitchScopes , "twitch-scopes" , defaultTwitchScopes , "Scopes for Twitch" )
159184 f .StringVar (& rootArgs .config .LinkedinClientID , "linkedin-client-id" , "" , "Client ID for Linkedin" )
160185 f .StringVar (& rootArgs .config .LinkedinClientSecret , "linkedin-client-secret" , "" , "Client secret for Linkedin" )
161- f .StringSliceVar (& rootArgs .config .LinkedinScopes , "linkedin-scopes" , [] string { "r_liteprofile" , "r_emailaddress" } , "Scopes for Linkedin" )
186+ f .StringSliceVar (& rootArgs .config .LinkedinScopes , "linkedin-scopes" , defaultLinkedinScopes , "Scopes for Linkedin" )
162187 f .StringVar (& rootArgs .config .AppleClientID , "apple-client-id" , "" , "Client ID for Apple" )
163188 f .StringVar (& rootArgs .config .AppleClientSecret , "apple-client-secret" , "" , "Client secret for Apple" )
164- f .StringSliceVar (& rootArgs .config .AppleScopes , "apple-scopes" , [] string { "email" , "name" } , "Scopes for Apple" )
189+ f .StringSliceVar (& rootArgs .config .AppleScopes , "apple-scopes" , defaultAppleScopes , "Scopes for Apple" )
165190 f .StringVar (& rootArgs .config .DiscordClientID , "discord-client-id" , "" , "Client ID for Discord" )
166191 f .StringVar (& rootArgs .config .DiscordClientSecret , "discord-client-secret" , "" , "Client secret for Discord" )
167- f .StringSliceVar (& rootArgs .config .DiscordScopes , "discord-scopes" , [] string { "identify" , "email" } , "Scopes for Discord" )
192+ f .StringSliceVar (& rootArgs .config .DiscordScopes , "discord-scopes" , defaultDiscordScopes , "Scopes for Discord" )
168193 f .StringVar (& rootArgs .config .TwitterClientID , "twitter-client-id" , "" , "Client ID for Twitter" )
169194 f .StringVar (& rootArgs .config .TwitterClientSecret , "twitter-client-secret" , "" , "Client secret for Twitter" )
170- f .StringSliceVar (& rootArgs .config .TwitterScopes , "twitter-scopes" , [] string { "tweet.read" , "users.read" } , "Scopes for Twitter" )
195+ f .StringSliceVar (& rootArgs .config .TwitterScopes , "twitter-scopes" , defaultTwitterScopes , "Scopes for Twitter" )
171196 f .StringVar (& rootArgs .config .RobloxClientID , "roblox-client-id" , "" , "Client ID for Roblox" )
172197 f .StringVar (& rootArgs .config .RobloxClientSecret , "roblox-client-secret" , "" , "Client secret for Roblox" )
173- f .StringSliceVar (& rootArgs .config .RobloxScopes , "roblox-scopes" , [] string { "openid" , "profile" } , "Scopes for Roblox" )
198+ f .StringSliceVar (& rootArgs .config .RobloxScopes , "roblox-scopes" , defaultRobloxScopes , "Scopes for Roblox" )
174199
175200 // URLs
176201 f .StringVar (& rootArgs .config .ResetPasswordURL , "reset-password-url" , "" , "URL for reset password" )
@@ -183,8 +208,87 @@ func init() {
183208 f .MarkDeprecated ("redis_url" , "use --redis-url instead" )
184209}
185210
211+ // applyFlagDefaults sets config and server fields to their flag defaults when the
212+ // value is empty (e.g. when user passes --host="" we use the default from vars above).
213+ func applyFlagDefaults () {
214+ c := & rootArgs .config
215+ s := & rootArgs .server
216+
217+ if s .HTTPPort == 0 {
218+ s .HTTPPort = defaultHTTPPort
219+ }
220+ if s .MetricsPort == 0 {
221+ s .MetricsPort = defaultMetricsPort
222+ }
223+ if strings .TrimSpace (rootArgs .logLevel ) == "" {
224+ rootArgs .logLevel = defaultLogLevel
225+ }
226+ if strings .TrimSpace (c .OrganizationLogo ) == "" {
227+ c .OrganizationLogo = defaultOrganizationLogo
228+ }
229+ if strings .TrimSpace (c .OrganizationName ) == "" {
230+ c .OrganizationName = defaultOrganizationName
231+ }
232+ if strings .TrimSpace (c .DefaultAuthorizeResponseMode ) == "" {
233+ c .DefaultAuthorizeResponseMode = constants .ResponseModeQuery
234+ }
235+ if strings .TrimSpace (c .DefaultAuthorizeResponseType ) == "" {
236+ c .DefaultAuthorizeResponseType = constants .ResponseTypeToken
237+ }
238+ if strings .TrimSpace (c .AdminSecret ) == "" {
239+ c .AdminSecret = defaultAdminSecret
240+ }
241+ if len (c .AllowedOrigins ) == 0 {
242+ c .AllowedOrigins = append ([]string (nil ), defaultAllowedOrigins ... )
243+ }
244+ if len (c .DefaultRoles ) == 0 {
245+ c .DefaultRoles = append ([]string (nil ), defaultRoles ... )
246+ }
247+ if len (c .Roles ) == 0 {
248+ c .Roles = append ([]string (nil ), defaultRoles ... )
249+ }
250+ if strings .TrimSpace (c .JWTRoleClaim ) == "" {
251+ c .JWTRoleClaim = defaultJWTRoleClaim
252+ }
253+ if strings .TrimSpace (c .MicrosoftTenantID ) == "" {
254+ c .MicrosoftTenantID = defaultMicrosoftTenantID
255+ }
256+ if len (c .GoogleScopes ) == 0 {
257+ c .GoogleScopes = append ([]string (nil ), defaultGoogleScopes ... )
258+ }
259+ if len (c .GithubScopes ) == 0 {
260+ c .GithubScopes = append ([]string (nil ), defaultGithubScopes ... )
261+ }
262+ if len (c .FacebookScopes ) == 0 {
263+ c .FacebookScopes = append ([]string (nil ), defaultFacebookScopes ... )
264+ }
265+ if len (c .MicrosoftScopes ) == 0 {
266+ c .MicrosoftScopes = append ([]string (nil ), defaultMicrosoftScopes ... )
267+ }
268+ if len (c .TwitchScopes ) == 0 {
269+ c .TwitchScopes = append ([]string (nil ), defaultTwitchScopes ... )
270+ }
271+ if len (c .LinkedinScopes ) == 0 {
272+ c .LinkedinScopes = append ([]string (nil ), defaultLinkedinScopes ... )
273+ }
274+ if len (c .AppleScopes ) == 0 {
275+ c .AppleScopes = append ([]string (nil ), defaultAppleScopes ... )
276+ }
277+ if len (c .DiscordScopes ) == 0 {
278+ c .DiscordScopes = append ([]string (nil ), defaultDiscordScopes ... )
279+ }
280+ if len (c .TwitterScopes ) == 0 {
281+ c .TwitterScopes = append ([]string (nil ), defaultTwitterScopes ... )
282+ }
283+ if len (c .RobloxScopes ) == 0 {
284+ c .RobloxScopes = append ([]string (nil ), defaultRobloxScopes ... )
285+ }
286+ }
287+
186288// Run the service
187289func runRoot (c * cobra.Command , args []string ) {
290+ applyFlagDefaults ()
291+
188292 // Prepare logger
189293 ctx := context .Background ()
190294 // Parse the log level
0 commit comments