/
user_controller.go
100 lines (90 loc) · 3.43 KB
/
user_controller.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package web
import (
"errors"
"fmt"
"net/http"
"github.com/ethereum/go-ethereum/accounts"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/presenters"
"github.com/smartcontractkit/chainlink/core/utils"
)
// UserController manages the current Session's User User.
type UserController struct {
App services.Application
}
// UpdatePassword changes the password for the current User.
func (c *UserController) UpdatePassword(ctx *gin.Context) {
var request models.ChangePasswordRequest
if err := ctx.ShouldBindJSON(&request); err != nil {
jsonAPIError(ctx, http.StatusUnprocessableEntity, err)
} else if user, err := c.App.GetStore().FindUser(); err != nil {
jsonAPIError(ctx, http.StatusInternalServerError, fmt.Errorf("failed to obtain current user record: %+v", err))
} else if !utils.CheckPasswordHash(request.OldPassword, user.HashedPassword) {
jsonAPIError(ctx, http.StatusConflict, errors.New("Old password does not match"))
} else if err := c.updateUserPassword(ctx, &user, request.NewPassword); err != nil {
jsonAPIError(ctx, http.StatusInternalServerError, err)
} else {
jsonAPIResponse(ctx, presenters.UserPresenter{User: &user}, "user")
}
}
// AccountBalances returns the account balances of ETH & LINK.
// Example:
// "<application>/user/balances"
func (c *UserController) AccountBalances(ctx *gin.Context) {
store := c.App.GetStore()
accounts := store.KeyStore.Accounts()
balances := []presenters.AccountBalance{}
for _, a := range accounts {
pa := getAccountBalanceFor(ctx, store, a)
if ctx.IsAborted() {
return
}
balances = append(balances, pa)
}
jsonAPIResponse(ctx, balances, "balances")
}
func (c *UserController) getCurrentSessionID(ctx *gin.Context) (string, error) {
session := sessions.Default(ctx)
sessionID, ok := session.Get(SessionIDKey).(string)
if !ok {
return "", errors.New("unable to get current session ID")
}
return sessionID, nil
}
func (c *UserController) saveNewPassword(user *models.User, newPassword string) error {
hashedPassword, err := utils.HashPassword(newPassword)
if err != nil {
return err
}
user.HashedPassword = hashedPassword
return c.App.GetStore().SaveUser(user)
}
func (c *UserController) updateUserPassword(ctx *gin.Context, user *models.User, newPassword string) error {
if sessionID, err := c.getCurrentSessionID(ctx); err != nil {
return err
} else if err := c.App.GetStore().ClearNonCurrentSessions(sessionID); err != nil {
return fmt.Errorf("failed to clear non current user sessions: %+v", err)
} else if err := c.saveNewPassword(user, newPassword); err != nil {
return fmt.Errorf("failed to update current user password: %+v", err)
}
return nil
}
func getAccountBalanceFor(ctx *gin.Context, store *store.Store, account accounts.Account) presenters.AccountBalance {
txm := store.TxManager
if ethBalance, err := txm.GetEthBalance(account.Address); err != nil {
jsonAPIError(ctx, http.StatusInternalServerError, err)
} else if linkBalance, err := txm.GetLINKBalance(account.Address); err != nil {
jsonAPIError(ctx, http.StatusInternalServerError, err)
} else {
return presenters.AccountBalance{
Address: account.Address.Hex(),
EthBalance: ethBalance,
LinkBalance: linkBalance,
}
}
return presenters.AccountBalance{}
}