-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
98 lines (87 loc) · 2.59 KB
/
message.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
package main
import (
"crypto/rsa"
"errors"
"fmt"
"forent_api/app"
"forent_api/models"
"io/ioutil"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware/security/jwt"
)
// MessageController implements the message resource.
type MessageController struct {
*goa.Controller
privateKey *rsa.PrivateKey
}
// NewMessageController creates a message controller.
func NewMessageController(service *goa.Service) (*MessageController, error) {
b, err := ioutil.ReadFile("./jwtkey/jwt.key")
if err != nil {
return nil, err
}
privKey, err := jwtgo.ParseRSAPrivateKeyFromPEM(b)
if err != nil {
return nil, fmt.Errorf("jwt: failed to load private key: %s", err) // bug
}
return &MessageController{
Controller: service.NewController("MessageController"),
privateKey: privKey,
}, nil
}
// Create runs the create action.
func (c *MessageController) Create(ctx *app.CreateMessageContext) error {
payload := ctx.Payload
token := jwt.ContextJWT(ctx)
if token == nil {
return fmt.Errorf("JWT token is missing from context") // internal error
}
if claims, ok := token.Claims.(jwtgo.MapClaims); ok && token.Valid {
offer, _ := OfferDB.OneOffer(ctx.Context, payload.OfferID, 0, 0)
if float64(offer.UserID) == claims["user_id"] {
message := models.Message{}
message.OfferID = payload.OfferID
message.UserID = offer.UserID
message.Text = payload.Text
err := MessageDB.Add(ctx.Context, &message)
if err != nil {
return ErrDatabaseError(err)
}
return ctx.Created()
}
if float64(offer.OwnerID) == claims["user_id"] {
message := models.Message{}
message.OfferID = payload.OfferID
message.UserID = offer.OwnerID
message.Text = payload.Text
err := MessageDB.Add(ctx.Context, &message)
if err != nil {
return ErrDatabaseError(err)
}
return ctx.Created()
}
}
errID := errors.New("id error")
return ctx.BadRequest(errID)
}
// List runs the list action.
func (c *MessageController) List(ctx *app.ListMessageContext) error {
payload := ctx.Payload
if payload.OfferID == 0 {
return ctx.NotFound()
}
token := jwt.ContextJWT(ctx)
if token == nil {
return fmt.Errorf("JWT token is missing from context") // internal error
}
if claims, ok := token.Claims.(jwtgo.MapClaims); ok && token.Valid {
offer, _ := OfferDB.OneOffer(ctx.Context, payload.OfferID, 0, 0)
if float64(offer.UserID) == claims["user_id"] || float64(offer.OwnerID) == claims["user_id"] {
messages := MessageDB.ListMessage(ctx.Context, payload.OfferID, 0)
return ctx.OK(messages)
}
}
errID := errors.New("id error")
return ctx.BadRequest(errID)
}