-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (197 loc) · 6.32 KB
/
main.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/CelesteComet/celeste-auth-server/pkg/middleware"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/google"
"golang.org/x/crypto/bcrypt"
"gopkg.in/matryer/respond.v1"
)
type User struct {
ID int `json:"id"`
Email string `json:"email"`
Password string `json:"password"`
DisplayName string `json:"display_name" db:"display_name"`
}
type UserHandler struct {
DB *sql.DB
}
type ErrorMessages struct {
ServerError string
BadCreds string
}
var errorMessages ErrorMessages = ErrorMessages{
ServerError: "Server error, please try again later",
BadCreds: "Username or Password is Incorrect",
}
func (h *UserHandler) Create() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
})
}
func (h *UserHandler) FindByCredentials() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromRequest(r)
if err != nil {
errors := []string{errorMessages.BadCreds}
respond.With(w, r, http.StatusUnauthorized, errors)
return
}
// Look for the user in the database
dbUser := User{}
h.DB.QueryRow("select * from member where email = $1", user.Email).Scan(&dbUser.ID, &dbUser.Email, &dbUser.Password, &dbUser.DisplayName)
if &dbUser.Email == nil {
errors := []string{errorMessages.BadCreds}
respond.With(w, r, http.StatusUnauthorized, errors)
return
}
// decode password
err = bcrypt.CompareHashAndPassword([]byte(dbUser.Password), []byte(user.Password))
if err != nil {
errors := []string{"Email or Password is Incorrect"}
log.Println("THIS IS HAPPENING")
respond.With(w, r, http.StatusUnauthorized, errors)
return
}
tokenString := h.ProvideToken(&dbUser, w)
// Set HttpOnly To Prevent Future Tampering
http.SetCookie(w, &http.Cookie{
Name: "jwt",
Value: tokenString,
HttpOnly: true,
Path: "/",
})
respond.With(w, r, http.StatusOK, &dbUser)
})
}
func (h *UserHandler) FindOrCreate() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromRequest(r)
log.Println(r.Body)
log.Println(user)
if err != nil {
errors := []string{errorMessages.BadCreds}
respond.With(w, r, http.StatusUnauthorized, errors)
return
}
// Look for the user in the database
dbUser := User{}
h.DB.QueryRow("select * from member where email = $1", user.Email).Scan(&dbUser.ID, &dbUser.Email, &dbUser.Password)
if &dbUser.Email == nil {
errors := []string{errorMessages.BadCreds}
respond.With(w, r, http.StatusUnauthorized, errors)
return
}
// decode password
// err = bcrypt.CompareHashAndPassword([]byte(dbUser.Password), []byte(user.Password))
// if (err != nil) {
// errors := []string{"Email or Password is Incorrect"}
// respond.With(w, r, http.StatusUnauthorized, errors)
// return
// }
h.ProvideToken(&dbUser, w)
respond.With(w, r, http.StatusOK, &dbUser)
})
}
func (h *UserHandler) ProvideToken(u *User, w http.ResponseWriter) string {
// Create a new token object, specifying signing method and the claims
// you would like it to contain.
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"id": u.ID,
"email": u.Email,
"display_name": u.DisplayName,
})
// Sign and get the complete encoded token as a string using the secret
tokenString, _ := token.SignedString([]byte("secret"))
w.Header().Set("JWT", tokenString)
return tokenString
}
func (h *UserHandler) LoginWithGoogle() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := gothic.CompleteUserAuth(w, r); err == nil {
// t, _ := template.New("foo").Parse(userTemplate)
// t.Execute(res, gothUser)
} else {
gothic.BeginAuthHandler(w, r)
}
})
}
func (h *UserHandler) handleOAuthCallback() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("HANDLE THAT GOOGLE CALLBACK")
if gothUser, err := gothic.CompleteUserAuth(w, r); err == nil {
// Look for the user in the database, if the user exists, return the user
dbUser := User{}
h.DB.QueryRow("select * from member where email = $1", gothUser.Email).Scan(&dbUser.ID, &dbUser.Email, &dbUser.Password)
if &dbUser.Email == nil {
errors := []string{errorMessages.BadCreds}
respond.With(w, r, http.StatusUnauthorized, errors)
return
}
h.ProvideToken(&dbUser, w)
respond.With(w, r, http.StatusOK, &dbUser)
}
})
}
// HELPER METHODS
func getUserFromRequest(r *http.Request) (User, error) {
decoder := json.NewDecoder(r.Body)
user := User{}
err := decoder.Decode(&user)
if err != nil {
return user, err
}
return user, nil
}
// Database Setup
var (
host = "celestecomet.c7bjz8zer8ha.us-east-1.rds.amazonaws.com"
port = 5432
user = os.Getenv("AWS_DB_USERNAME")
password = os.Getenv("AWS_DB_PASSWORD")
dbname = "CelesteComet"
)
var (
connStr = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
)
func main() {
// Initialize Database
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
log.Println("Server connection successful")
defer db.Close()
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Model Handlers
userHandler := UserHandler{DB: db}
router := mux.NewRouter()
// Middleware
router.Use(middleware.Cors)
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
// OAuth
goth.UseProviders(
google.New(os.Getenv("GOOGLE_KEY"), os.Getenv("GOOGLE_SECRET"), "http://localhost:1337/auth/google/callback"),
)
// Routes
router.Handle("/users", userHandler.Create()).Methods("POST")
router.Handle("/login", userHandler.FindByCredentials()).Methods("POST", "OPTIONS")
router.Handle("/oauth", userHandler.FindOrCreate()).Methods("POST")
router.Handle("/auth/{provider}", userHandler.LoginWithGoogle()).Methods("GET")
router.Handle("/auth/{provider}/callback", userHandler.handleOAuthCallback()).Methods("GET")
server := &http.Server{
Handler: loggedRouter,
Addr: ":1337",
}
log.Println("Server is now running on port 1337")
server.ListenAndServe()
}