@@ -112,6 +112,20 @@ type AccountEvent struct {
112112 Data string `json:"data"`
113113}
114114
115+ type authEventType string
116+
117+ const (
118+ authRequired authEventType = "auth-required"
119+ authForced authEventType = "auth-forced"
120+ authCanceled authEventType = "auth-canceled"
121+ authOk authEventType = "auth-ok"
122+ authErr authEventType = "auth-err"
123+ )
124+
125+ type authEventObject struct {
126+ Typ authEventType `json:"typ"`
127+ }
128+
115129// Environment represents functionality where the implementation depends on the environment the app
116130// runs in, e.g. Qt5/Mobile/webdev.
117131type Environment interface {
@@ -143,6 +157,7 @@ type Environment interface {
143157 SetDarkTheme (bool )
144158 // DetectDarkTheme returns true if the dark theme is enabled at OS level.
145159 DetectDarkTheme () bool
160+ Auth ()
146161}
147162
148163// Backend ties everything together and is the main starting point to use the BitBox wallet library.
@@ -296,6 +311,79 @@ func (backend *Backend) Config() *config.Config {
296311 return backend .config
297312}
298313
314+ // Authenticate executes a system authentication if
315+ // the authentication config flag is enabled or if the
316+ // `force` input flag is enabled (as a consequence of an
317+ // 'auth/auth-forced' notification).
318+ // Otherwise, the authentication is automatically assumed as
319+ // successful.
320+ func (backend * Backend ) Authenticate (force bool ) {
321+ backend .log .Info ("Auth requested" )
322+ if backend .config .AppConfig ().Backend .Authentication || force {
323+ backend .environment .Auth ()
324+ } else {
325+ backend .AuthResult (true )
326+ }
327+ }
328+
329+ // TriggerAuth triggers an auth-required notification.
330+ func (backend * Backend ) TriggerAuth () {
331+ backend .Notify (observable.Event {
332+ Subject : "auth" ,
333+ Action : action .Replace ,
334+ Object : authEventObject {
335+ Typ : authRequired ,
336+ },
337+ })
338+ }
339+
340+ // CancelAuth triggers an auth-canceled notification.
341+ func (backend * Backend ) CancelAuth () {
342+ backend .Notify (observable.Event {
343+ Subject : "auth" ,
344+ Action : action .Replace ,
345+ Object : authEventObject {
346+ Typ : authCanceled ,
347+ },
348+ })
349+ }
350+
351+ // ForceAuth triggers an auth-forced notification
352+ // followed by an auth-required notification.
353+ func (backend * Backend ) ForceAuth () {
354+ backend .Notify (observable.Event {
355+ Subject : "auth" ,
356+ Action : action .Replace ,
357+ Object : authEventObject {
358+ Typ : authForced ,
359+ },
360+ })
361+ backend .Notify (observable.Event {
362+ Subject : "auth" ,
363+ Action : action .Replace ,
364+ Object : authEventObject {
365+ Typ : authRequired ,
366+ },
367+ })
368+ }
369+
370+ // AuthResult triggers an auth-ok or auth-err notification
371+ // depending on the input value.
372+ func (backend * Backend ) AuthResult (ok bool ) {
373+ backend .log .Infof ("Auth result: %v" , ok )
374+ typ := authErr
375+ if ok {
376+ typ = authOk
377+ }
378+ backend .Notify (observable.Event {
379+ Subject : "auth" ,
380+ Action : action .Replace ,
381+ Object : authEventObject {
382+ Typ : typ ,
383+ },
384+ })
385+ }
386+
299387// DefaultAppConfig returns the default app config.
300388func (backend * Backend ) DefaultAppConfig () config.AppConfig {
301389 return config .NewDefaultAppConfig ()
0 commit comments