|
| 1 | +package directadmin |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type credentials struct { |
| 12 | + username string |
| 13 | + passkey string |
| 14 | +} |
| 15 | + |
| 16 | +type LoginHistory struct { |
| 17 | + Attempts int `json:"attempts"` |
| 18 | + Host string `json:"host"` |
| 19 | + Timestamp time.Time `json:"timestamp"` |
| 20 | +} |
| 21 | + |
| 22 | +func (c *AdminContext) GetLoginHistory() ([]*LoginHistory, error) { |
| 23 | + var loginHistory []*LoginHistory |
| 24 | + |
| 25 | + if _, err := c.api.makeRequestN(http.MethodGet, "login-history", c.credentials, nil, &loginHistory); err != nil { |
| 26 | + return nil, fmt.Errorf("failed to get login history: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + if len(loginHistory) == 0 { |
| 30 | + return nil, fmt.Errorf("no login history found") |
| 31 | + } |
| 32 | + |
| 33 | + return loginHistory, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (c *UserContext) GetMyUsername() string { |
| 37 | + // if user is logged in via reseller, we need to remove the reseller username from the context's username |
| 38 | + if strings.Contains(c.credentials.username, "|") { |
| 39 | + return strings.Split(c.credentials.username, "|")[1] |
| 40 | + } |
| 41 | + |
| 42 | + return c.credentials.username |
| 43 | +} |
| 44 | + |
| 45 | +func (c *UserContext) Login() error { |
| 46 | + var response apiGenericResponse |
| 47 | + |
| 48 | + if _, err := c.api.makeRequest(http.MethodGet, "API_LOGIN_TEST", c.credentials, nil, &response); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + if response.Success != "Login OK" { |
| 53 | + return errors.New("login failed") |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// LoginAsAdmin verifies the provided credentials against the DA API, then returns an admin-level context. |
| 60 | +// The passkey can either be the user's password, or a login key |
| 61 | +func (a *API) LoginAsAdmin(username string, passkey string) (*AdminContext, error) { |
| 62 | + userCtx, err := a.login(username, passkey) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + adminCtx := AdminContext{ |
| 68 | + ResellerContext{ |
| 69 | + UserContext: *userCtx, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + if adminCtx.User.Config.UserType != AccountRoleAdmin { |
| 74 | + return nil, fmt.Errorf("account is not an Admin, it is a %v", adminCtx.User.Config.UserType) |
| 75 | + } |
| 76 | + |
| 77 | + return &adminCtx, nil |
| 78 | +} |
| 79 | + |
| 80 | +// LoginAsReseller verifies the provided credentials against the DA API, then returns a reseller-level context. |
| 81 | +// The passkey can either be the user's password, or a login key |
| 82 | +func (a *API) LoginAsReseller(username string, passkey string) (*ResellerContext, error) { |
| 83 | + userCtx, err := a.login(username, passkey) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + resellerCtx := ResellerContext{ |
| 89 | + UserContext: *userCtx, |
| 90 | + } |
| 91 | + |
| 92 | + if resellerCtx.User.Config.UserType != AccountRoleReseller { |
| 93 | + return nil, fmt.Errorf("account is not an Reseller, it is a %v", resellerCtx.User.Config.UserType) |
| 94 | + } |
| 95 | + |
| 96 | + return &resellerCtx, nil |
| 97 | +} |
| 98 | + |
| 99 | +// LoginAsUser verifies the provided credentials against the DA API, then returns a user-level context. |
| 100 | +// The passkey can either be the user's password, or a login key |
| 101 | +func (a *API) LoginAsUser(username string, passkey string) (*UserContext, error) { |
| 102 | + userCtx, err := a.login(username, passkey) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + if userCtx.User.Config.UserType != AccountRoleUser { |
| 108 | + return nil, fmt.Errorf("account is not a User, it is a %v", userCtx.User.Config.UserType) |
| 109 | + } |
| 110 | + |
| 111 | + return userCtx, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (c *AdminContext) LoginAsMyReseller(username string) (*ResellerContext, error) { |
| 115 | + return c.api.LoginAsReseller(c.credentials.username+"|"+username, c.credentials.passkey) |
| 116 | +} |
| 117 | + |
| 118 | +func (c *ResellerContext) LoginAsMyUser(username string) (*UserContext, error) { |
| 119 | + return c.api.LoginAsUser(c.credentials.username+"|"+username, c.credentials.passkey) |
| 120 | +} |
| 121 | + |
| 122 | +func (a *API) login(username string, passkey string) (*UserContext, error) { |
| 123 | + userCtx := UserContext{ |
| 124 | + api: a, |
| 125 | + credentials: credentials{ |
| 126 | + username: username, |
| 127 | + passkey: passkey, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + if err := userCtx.Login(); err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + userConfig, err := userCtx.GetMyUserConfig() |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + userCtx.User.Config = *userConfig |
| 141 | + |
| 142 | + return &userCtx, nil |
| 143 | +} |
0 commit comments