Two stage option parsing.
This commit is contained in:
		
							parent
							
								
									cf9c6220e6
								
							
						
					
					
						commit
						21480c5866
					
				
					 1 changed files with 90 additions and 25 deletions
				
			
		
							
								
								
									
										115
									
								
								src/main.cr
									
										
									
									
									
								
							
							
						
						
									
										115
									
								
								src/main.cr
									
										
									
									
									
								
							|  | @ -14,7 +14,27 @@ require "./authd.cr" | ||||||
| 
 | 
 | ||||||
| extend AuthD | extend AuthD | ||||||
| 
 | 
 | ||||||
|  | class Baguette::Configuration | ||||||
|  | 	class Auth < Base | ||||||
|  | 		property storage                : String? | ||||||
|  | 		property jwt_key                : String? | ||||||
|  | 		property registrations          : Bool? | ||||||
|  | 		property require_email          : Bool? | ||||||
|  | 		property activation_url         : String? | ||||||
|  | 		property field_subject          : String? | ||||||
|  | 		property field_from             : String? | ||||||
|  | 		property read_only_profile_keys : Array(String)? | ||||||
|  | 
 | ||||||
|  | 		property verbosity              : Int32? | ||||||
|  | 		property print_ipc_timer        : Bool? | ||||||
|  | 		property ipc_timer              : Int32? | ||||||
|  | 	end | ||||||
|  | end | ||||||
|  | 
 | ||||||
| class AuthD::Service | class AuthD::Service | ||||||
|  | 	property timer                 = 30_000  # 30 seconds | ||||||
|  | 	property print_timer           = false | ||||||
|  | 
 | ||||||
| 	property registrations_allowed = false | 	property registrations_allowed = false | ||||||
| 	property require_email         = false | 	property require_email         = false | ||||||
| 	property mailer_activation_url : String? = nil | 	property mailer_activation_url : String? = nil | ||||||
|  | @ -22,6 +42,7 @@ class AuthD::Service | ||||||
| 	property mailer_field_subject  : String? = nil | 	property mailer_field_subject  : String? = nil | ||||||
| 	property read_only_profile_keys = Array(String).new | 	property read_only_profile_keys = Array(String).new | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| 	@users_per_login : DODB::Index(User) | 	@users_per_login : DODB::Index(User) | ||||||
| 	@users_per_uid   : DODB::Index(User) | 	@users_per_uid   : DODB::Index(User) | ||||||
| 
 | 
 | ||||||
|  | @ -610,8 +631,8 @@ class AuthD::Service | ||||||
| 		## | 		## | ||||||
| 		# Provides a JWT-based authentication scheme for service-specific users. | 		# Provides a JWT-based authentication scheme for service-specific users. | ||||||
| 		server = IPC::Server.new "auth" | 		server = IPC::Server.new "auth" | ||||||
| 		server.base_timer = 30000 # 30 seconds | 		server.base_timer = @timer | ||||||
| 		server.timer      = 30000 # 30 seconds | 		server.timer      = @timer | ||||||
| 		server.loop do |event| | 		server.loop do |event| | ||||||
| 			if event.is_a? IPC::Exception | 			if event.is_a? IPC::Exception | ||||||
| 				Baguette::Log.error "IPC::Exception" | 				Baguette::Log.error "IPC::Exception" | ||||||
|  | @ -620,7 +641,7 @@ class AuthD::Service | ||||||
| 
 | 
 | ||||||
| 			case event | 			case event | ||||||
| 			when IPC::Event::Timer | 			when IPC::Event::Timer | ||||||
| 				Baguette::Log.debug "Timer" | 				Baguette::Log.debug "Timer" if @print_timer | ||||||
| 			when IPC::Event::MessageReceived | 			when IPC::Event::MessageReceived | ||||||
| 				begin | 				begin | ||||||
| 					request = Request.from_ipc(event.message).not_nil! | 					request = Request.from_ipc(event.message).not_nil! | ||||||
|  | @ -649,20 +670,55 @@ class AuthD::Service | ||||||
| 	end | 	end | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| authd_storage = "storage" | print_timer                       = false | ||||||
| authd_jwt_key = "nico-nico-nii" | timer                             = 30_000 | ||||||
| authd_registrations = false | authd_storage                     = "storage" | ||||||
| authd_require_email = false | authd_jwt_key                     = "nico-nico-nii" | ||||||
| activation_url : String? = nil | authd_registrations               = false | ||||||
| field_subject  : String? = nil | authd_require_email               = false | ||||||
| field_from     : String? = nil | activation_url          : String? = nil | ||||||
| read_only_profile_keys = Array(String).new | field_subject           : String? = nil | ||||||
|  | field_from              : String? = nil | ||||||
|  | read_only_profile_keys            = Array(String).new | ||||||
| 
 | 
 | ||||||
| begin | begin | ||||||
|  | 
 | ||||||
|  | 	simulation, no_configuration, configuration_file = Baguette::Configuration.option_parser | ||||||
|  | 
 | ||||||
|  | 	configuration = if no_configuration | ||||||
|  | 		Baguette::Log.info "do not load a configuration file." | ||||||
|  | 		nil | ||||||
|  | 	else | ||||||
|  | 		Baguette::Configuration::Auth.get | ||||||
|  | 	end | ||||||
|  | 
 | ||||||
|  | 	configuration.try do |conf| | ||||||
|  | 		Baguette::Context.verbosity = conf.verbosity.not_nil! unless conf.verbosity.nil? | ||||||
|  | 
 | ||||||
|  | 		if key_file = conf.shared_key_file | ||||||
|  | 			authd_jwt_key = File.read(key_file).chomp | ||||||
|  | 		end | ||||||
|  | 
 | ||||||
|  | 		if ro_profile_keys = conf.read_only_profile_keys | ||||||
|  | 			read_only_profile_keys = ro_profile_keys | ||||||
|  | 		end | ||||||
|  | 
 | ||||||
|  | 		print_timer          = conf.print_ipc_timer.not_nil! unless conf.print_ipc_timer.nil? | ||||||
|  | 		timer                = conf.ipc_timer.not_nil!       unless conf.ipc_timer.nil? | ||||||
|  | 		authd_jwt_key        = conf.shared_key.not_nil!      unless conf.shared_key.nil? | ||||||
|  | 		authd_storage        = conf.storage.not_nil!         unless conf.storage.nil? | ||||||
|  | 		authd_registrations  = conf.registrations.not_nil!   unless conf.registrations.nil? | ||||||
|  | 		authd_require_email  = conf.require_email.not_nil!   unless conf.require_email.nil? | ||||||
|  | 		activation_url       = conf.activation_url.not_nil!  unless conf.activation_url.nil? | ||||||
|  | 		field_subject        = conf.field_subject.not_nil!   unless conf.field_subject.nil? | ||||||
|  | 		field_from           = conf.field_from.not_nil!      unless conf.field_from.nil? | ||||||
|  | 	end | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| 	OptionParser.parse do |parser| | 	OptionParser.parse do |parser| | ||||||
| 		parser.banner = "usage: authd [options]" | 		parser.banner = "usage: authd [options]" | ||||||
| 
 | 
 | ||||||
| 		parser.on "-s directory", "--storage directory", "Directory in which to store users." do |directory| | 		parser.on "--storage directory", "Directory in which to store users." do |directory| | ||||||
| 			authd_storage = directory | 			authd_storage = directory | ||||||
| 		end | 		end | ||||||
| 
 | 
 | ||||||
|  | @ -694,27 +750,36 @@ begin | ||||||
| 			read_only_profile_keys.push key | 			read_only_profile_keys.push key | ||||||
| 		end | 		end | ||||||
| 
 | 
 | ||||||
| 		parser.on "-v verbosity", |  | ||||||
| 			"--verbosity level", |  | ||||||
| 			"Verbosity level. From 0 to 3. Default: 1" do |v| |  | ||||||
| 			Baguette::Context.verbosity = v.to_i |  | ||||||
| 		end |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 		parser.on "-h", "--help", "Show this help" do | 		parser.on "-h", "--help", "Show this help" do | ||||||
| 			puts parser | 			puts parser | ||||||
| 
 |  | ||||||
| 			exit 0 | 			exit 0 | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| 
 | 
 | ||||||
|  | 	if simulation | ||||||
|  | 		pp! Baguette::Context.verbosity | ||||||
|  | 		pp! print_timer | ||||||
|  | 		pp! timer | ||||||
|  | 		pp! authd_storage | ||||||
|  | 		pp! authd_jwt_key | ||||||
|  | 		pp! authd_registrations | ||||||
|  | 		pp! authd_require_email | ||||||
|  | 		pp! activation_url | ||||||
|  | 		pp! field_subject | ||||||
|  | 		pp! field_from | ||||||
|  | 		pp! read_only_profile_keys | ||||||
|  | 		exit 0 | ||||||
|  | 	end | ||||||
|  | 
 | ||||||
| 	AuthD::Service.new(authd_storage, authd_jwt_key).tap do |authd| | 	AuthD::Service.new(authd_storage, authd_jwt_key).tap do |authd| | ||||||
| 		authd.registrations_allowed = authd_registrations | 		authd.registrations_allowed  = authd_registrations | ||||||
| 		authd.require_email         = authd_require_email | 		authd.require_email          = authd_require_email | ||||||
| 		authd.mailer_activation_url = activation_url | 		authd.mailer_activation_url  = activation_url | ||||||
| 		authd.mailer_field_subject  = field_subject | 		authd.mailer_field_subject   = field_subject | ||||||
| 		authd.mailer_field_from     = field_from | 		authd.mailer_field_from      = field_from | ||||||
| 		authd.read_only_profile_keys = read_only_profile_keys | 		authd.read_only_profile_keys = read_only_profile_keys | ||||||
|  | 		authd.print_timer            = print_timer | ||||||
|  | 		authd.timer                  = timer | ||||||
| 	end.run | 	end.run | ||||||
| rescue e : OptionParser::Exception | rescue e : OptionParser::Exception | ||||||
| 	Baguette::Log.error e.message | 	Baguette::Log.error e.message | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue