/
anonymous_user_promotion_code.go
112 lines (98 loc) · 3.25 KB
/
anonymous_user_promotion_code.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
101
102
103
104
105
106
107
108
109
110
111
112
package api
import (
"net/http"
"time"
"github.com/authgear/authgear-server/pkg/api"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/lib/authn/identity/anonymous"
"github.com/authgear/authgear-server/pkg/lib/infra/db/appdb"
oauthhandler "github.com/authgear/authgear-server/pkg/lib/oauth/handler"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/httputil"
"github.com/authgear/authgear-server/pkg/util/log"
"github.com/authgear/authgear-server/pkg/util/validation"
)
func ConfigureAnonymousUserPromotionCodeRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("POST", "OPTIONS").
WithPathPattern("/api/anonymous_user/promotion_code")
}
var AnonymousUserPromotionCodeAPIRequestSchema = validation.NewSimpleSchema(`
{
"type": "object",
"additionalProperties": false,
"properties": {
"session_type": {
"type": "string",
"enum": ["cookie", "refresh_token"]
},
"refresh_token": { "type": "string" }
},
"required": ["session_type"]
}
`)
var AnonymousUserPromotionCodeResponseSchema = validation.NewSimpleSchema(`
{
"type": "object",
"properties": {
"promotion_code": { "type": "string" },
"expire_at": { "type": "string" }
},
"required": ["promotion_code", "expire_at"]
}
`)
type AnonymousUserPromotionCodeRequest struct {
SessionType oauthhandler.WebSessionType `json:"session_type"`
RefreshToken string `json:"refresh_token"`
}
type AnonymousUserPromotionCodeResponse struct {
PromotionCode string `json:"promotion_code"`
ExpireAt time.Time `json:"expire_at"`
}
type PromotionCodeIssuer interface {
IssuePromotionCode(
req *http.Request,
sessionType oauthhandler.WebSessionType,
refreshToken string,
) (code string, codeObj *anonymous.PromotionCode, err error)
}
type AnonymousUserPromotionCodeAPIHandlerLogger struct{ *log.Logger }
func NewAnonymousUserPromotionCodeAPILogger(lf *log.Factory) AnonymousUserPromotionCodeAPIHandlerLogger {
return AnonymousUserPromotionCodeAPIHandlerLogger{lf.New("handler-anonymous-user-promotion-code")}
}
type AnonymousUserPromotionCodeAPIHandler struct {
Logger AnonymousUserPromotionCodeAPIHandlerLogger
Database *appdb.Handle
JSON JSONResponseWriter
PromotionCodes PromotionCodeIssuer
}
func (h *AnonymousUserPromotionCodeAPIHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
var payload AnonymousUserPromotionCodeRequest
err := httputil.BindJSONBody(req, resp, AnonymousUserPromotionCodeAPIRequestSchema.Validator(), &payload)
if err != nil {
h.JSON.WriteResponse(resp, &api.Response{Error: err})
return
}
result := &AnonymousUserPromotionCodeResponse{}
err = h.Database.WithTx(func() error {
code, codeObj, err := h.PromotionCodes.IssuePromotionCode(
req,
payload.SessionType,
payload.RefreshToken,
)
if err != nil {
return err
}
result.PromotionCode = code
result.ExpireAt = codeObj.ExpireAt
return nil
})
if err == nil {
h.JSON.WriteResponse(resp, &api.Response{Result: result})
} else {
if !apierrors.IsAPIError(err) {
h.Logger.WithError(err).Error("anonymous user promotion code handler failed")
}
h.JSON.WriteResponse(resp, &api.Response{Error: err})
}
}