-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
141 lines (117 loc) · 3.89 KB
/
main.go
File metadata and controls
141 lines (117 loc) · 3.89 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"log"
"net/http"
"regexp"
"github.com/gorilla/sessions"
"github.com/sirupsen/logrus"
"encoding/base64"
"github.com/gin-gonic/gin"
adapter "github.com/gwatts/gin-adapter"
"github.com/jesusvazquez/authboss-gin-sample/abclientstate"
"github.com/volatiletech/authboss"
"github.com/volatiletech/authboss/defaults"
)
var (
host = "127.0.0.1"
port = "3000"
database = NewMemStorer()
sessionStore abclientstate.SessionStorer
cookieStore abclientstate.CookieStorer
)
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(gin.Recovery())
cookieStoreKey, _ := base64.StdEncoding.DecodeString(`NpEPi8pEjKVjLGJ6kYCS+VTCzi6BUuDzU0wrwXyf5uDPArtlofn2AG6aTMiPmN3C909rsEWMNqJqhIVPGP3Exg==`)
sessionStoreKey, _ := base64.StdEncoding.DecodeString(`AbfYwmmt8UCwUuhd9qvfNA9UCuN1cVcKJN1ofbiky6xCyyBj20whe40rJa3Su0WOWLWcPpO1taqJdsEI/65+JA==`)
cookieStore = abclientstate.NewCookieStorer(cookieStoreKey, nil)
cookieStore.HTTPOnly = false
cookieStore.Secure = false
sessionStore = abclientstate.NewSessionStorer("test", sessionStoreKey, nil)
cstore := sessionStore.Store.(*sessions.CookieStore)
cstore.Options.HttpOnly = false
cstore.Options.Secure = false
// Setting up Authboss
ab := initAuthBossParam()
// Set up LoadClientStateMiddleware, required by the auth module
router.Use(adapter.Wrap(ab.LoadClientStateMiddleware))
// Init all the auth routes to go to authboss
router.Any("/auth/*w", gin.WrapH(ab.Config.Core.Router))
// Default endpoints
router.GET("/status", status)
// Initialize http server
server := &http.Server{
Addr: host + ":" + port,
Handler: router,
}
// DEBUG SECTION
fmt.Println("## Variables at the top")
fmt.Printf("- Host: %s - Port: %s\n", host, port)
fmt.Println("## Debugging authboss")
fmt.Printf("- ab.Config.Paths.RootURL: %s\n", ab.Config.Paths.RootURL)
fmt.Printf("- ab.Config.Paths.Mount: %s\n", ab.Config.Paths.Mount)
fmt.Printf("- Auth Module Loaded?: %v\n", ab.IsLoaded("auth"))
fmt.Println("## Gin Routes loaded")
routes := router.Routes()
for route := range routes {
fmt.Printf("- %s\n", routes[route].Path)
}
// Start serving traffic
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logrus.Fatalf("listen: %s\n", err)
}
}
// initializes authboss parameters
func initAuthBossParam() *authboss.Authboss {
ab := authboss.New()
ab.Config.Paths.RootURL = "http://" + host + ":" + port
ab.Config.Storage.Server = database
ab.Config.Storage.SessionState = sessionStore
ab.Config.Storage.CookieState = cookieStore
ab.Config.Paths.Mount = "/auth"
// Default to API usage
ab.Config.Core.ViewRenderer = defaults.JSONRenderer{}
defaults.SetCore(&ab.Config, true, false)
emailRule := defaults.Rules{
FieldName: "email", Required: true,
MatchError: "Must be a valid e-mail address",
MustMatch: regexp.MustCompile(`.*@.*\.[a-z]{1,}`),
}
passwordRule := defaults.Rules{
FieldName: "password", Required: true,
MinLength: 4,
}
nameRule := defaults.Rules{
FieldName: "name", Required: true,
MinLength: 2,
}
ab.Config.Core.BodyReader = defaults.HTTPBodyReader{
ReadJSON: true,
Rulesets: map[string][]defaults.Rules{
"register": {emailRule, passwordRule, nameRule},
"recover_end": {passwordRule},
},
Confirms: map[string][]string{
"register": {"password", authboss.ConfirmPrefix + "password"},
"recover_end": {"password", authboss.ConfirmPrefix + "password"},
},
Whitelist: map[string][]string{
"register": []string{"email", "name", "password"},
},
}
// Note: here we could instantiate more modules like sms, oauth, twofactor...
if err := ab.Init(); err != nil {
// Handle error, don't let program continue to run
log.Fatalln(err)
}
return ab
}
// status endpoint to expose service livenessprobe
func status(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}