File: two_factor_auth.go

package info (click to toggle)
golang-github-packethost-packngo 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 440 kB
  • sloc: makefile: 2
file content (56 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package packngo

const twoFactorAuthAppPath = "/user/otp/app"
const twoFactorAuthSmsPath = "/user/otp/sms"

// TwoFactorAuthService interface defines available two factor authentication functions
type TwoFactorAuthService interface {
	EnableApp(string) (*Response, error)
	DisableApp(string) (*Response, error)
	EnableSms(string) (*Response, error)
	DisableSms(string) (*Response, error)
	ReceiveSms() (*Response, error)
	SeedApp() (string, *Response, error)
}

// TwoFactorAuthServiceOp implements TwoFactorAuthService
type TwoFactorAuthServiceOp struct {
	client *Client
}

// EnableApp function enables two factor auth using authenticatior app
func (s *TwoFactorAuthServiceOp) EnableApp(token string) (resp *Response, err error) {
	headers := map[string]string{"x-otp-token": token}
	return s.client.DoRequestWithHeader("POST", headers, twoFactorAuthAppPath, nil, nil)
}

// EnableSms function enables two factor auth using sms
func (s *TwoFactorAuthServiceOp) EnableSms(token string) (resp *Response, err error) {
	headers := map[string]string{"x-otp-token": token}
	return s.client.DoRequestWithHeader("POST", headers, twoFactorAuthSmsPath, nil, nil)
}

// ReceiveSms orders the auth service to issue an SMS token
func (s *TwoFactorAuthServiceOp) ReceiveSms() (resp *Response, err error) {
	return s.client.DoRequest("POST", twoFactorAuthSmsPath+"/receive", nil, nil)
}

// DisableApp function disables two factor auth using
func (s *TwoFactorAuthServiceOp) DisableApp(token string) (resp *Response, err error) {
	headers := map[string]string{"x-otp-token": token}
	return s.client.DoRequestWithHeader("DELETE", headers, twoFactorAuthAppPath, nil, nil)
}

// DisableSms function disables two factor auth using
func (s *TwoFactorAuthServiceOp) DisableSms(token string) (resp *Response, err error) {
	headers := map[string]string{"x-otp-token": token}
	return s.client.DoRequestWithHeader("DELETE", headers, twoFactorAuthSmsPath, nil, nil)
}

// SeedApp orders the auth service to issue a token via google authenticator
func (s *TwoFactorAuthServiceOp) SeedApp() (otpURI string, resp *Response, err error) {
	ret := &map[string]string{}
	resp, err = s.client.DoRequest("POST", twoFactorAuthAppPath+"/receive", nil, ret)

	return (*ret)["otp_uri"], resp, err
}