44 "fmt"
55 "log"
66 "net/http"
7+ "slices"
78 "sort"
89 "sync"
910
@@ -55,7 +56,7 @@ func EnableMethods(method ...types.MFA) {
5556 }
5657}
5758
58- func ReinitaliseMethods (firewall * router. Firewall , method ... types.MFA ) ([]types.MFA , error ) {
59+ func ReinitaliseMethods (method ... types.MFA ) ([]types.MFA , error ) {
5960 lck .Lock ()
6061 defer lck .Unlock ()
6162
@@ -64,7 +65,7 @@ func ReinitaliseMethods(firewall *router.Firewall, method ...types.MFA) ([]types
6465 var errRet error
6566 for _ , m := range method {
6667 if a , ok := allMfa [m ]; ok {
67- err := a .Init ( firewall )
68+ err := a .ReloadSettings ( )
6869 if err != nil {
6970 if errRet == nil {
7071 errRet = fmt .Errorf ("%s failed to init: %s" , m , err )
@@ -112,6 +113,7 @@ func GetAllEnabledMethods() (r []Authenticator) {
112113 return
113114}
114115
116+ // GetAllAvaliableMethods returns All implemented authenticators in wag
115117func GetAllAvaliableMethods () (r []Authenticator ) {
116118 lck .RLock ()
117119 defer lck .RUnlock ()
@@ -133,69 +135,55 @@ func AddMFARoutes(mux *http.ServeMux, firewall *router.Firewall) error {
133135 lck .Lock ()
134136 defer lck .Unlock ()
135137
136- for method , handler := range allMfa {
137- mux .HandleFunc ("GET /authorise/" + string (method )+ "/" , checkEnabled (handler , handler .AuthorisationAPI ))
138- mux .HandleFunc ("POST /authorise/" + string (method )+ "/" , checkEnabled (handler , handler .AuthorisationAPI ))
139- mux .HandleFunc ("GET /register_mfa/" + string (method )+ "/" , checkEnabled (handler , handler .RegistrationAPI ))
140- mux .HandleFunc ("POST /register_mfa/" + string (method )+ "/" , checkEnabled (handler , handler .RegistrationAPI ))
141-
142- }
143-
144138 enabledMethods , err := data .GetEnabledAuthenicationMethods ()
145139 if err != nil {
146140 return err
147141 }
148142
149- for _ , method := range enabledMethods {
150- err := allMfa [types .MFA (method )].Init (firewall )
143+ for method , handler := range allMfa {
144+ prefix := "/api/" + string (method )
145+ r , err := handler .Routes (firewall , slices .Contains (enabledMethods , string (method )))
151146 if err != nil {
152147 log .Println ("failed to initialise method: " , method , "err: " , err )
153148 continue
154149 }
155- allMfa [types .MFA (method )].Enable ()
150+
151+ mux .Handle (prefix , http .StripPrefix (prefix , checkEnabled (r , allMfa [method ])))
156152 }
157153
158154 return nil
159155}
160156
161- func checkEnabled (a Authenticator , f func (w http.ResponseWriter , r * http.Request )) func (w http.ResponseWriter , r * http.Request ) {
162- return func (w http.ResponseWriter , r * http.Request ) {
157+ type enabled struct {
158+ next http.Handler
159+ auth Authenticator
160+ }
163161
164- if ! a .IsEnabled () {
165- http .NotFound (w , r )
166- return
167- }
162+ func (d * enabled ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
163+ d .next .ServeHTTP (w , r )
164+ }
168165
169- f (w , r )
166+ func checkEnabled (next http.Handler , auth Authenticator ) http.Handler {
167+ return & enabled {
168+ next : next ,
169+ auth : auth ,
170170 }
171171}
172172
173173type Authenticator interface {
174- Init (fw * router.Firewall ) error
175-
176174 IsEnabled () bool
175+
177176 Enable ()
178177 Disable ()
179178
179+ ReloadSettings () error
180+
180181 Type () string
181182
182183 //FriendlyName is the name that is displayed in the MFA selection table
183184 FriendlyName () string
184185
185- //LogoutPath returns the redirection path that deauthenticates selected mfa method (mostly just "/" unless it's externally connected to something)
186- LogoutPath () string
187-
188- //RegistrationAPI automatically added under /register_mfa/<mfa_method_name>
189- RegistrationAPI (w http.ResponseWriter , r * http.Request )
190-
191- //AuthorisationAPI automatically added under /authorise/<mfa_method_name>
192- AuthorisationAPI (w http.ResponseWriter , r * http.Request )
193-
194- //MFAPromptUI is executed in /authorise/ path to display UI when user browses to that path
195- MFAPromptUI (w http.ResponseWriter , r * http.Request , username , ip string )
196-
197- //RegistrationUI is executed in /register_mfa/ path to show the UI for registration
198- RegistrationUI (w http.ResponseWriter , r * http.Request , username , ip string )
186+ Routes (fw * router.Firewall , initiallyEnabled bool ) (* http.ServeMux , error )
199187}
200188
201189func StringsToMFA (methods []string ) (ret []types.MFA ) {
0 commit comments