11package main
22
33import (
4+ "fmt"
45 "html/template"
56 "log"
67 "net/http"
78 "time"
89
10+ "github.com/boltdb/bolt"
911 "github.com/julienschmidt/httprouter"
1012)
1113
14+ // TODO add bcrypt
15+
16+ func init () {
17+ // Handles db/bucket creation
18+ db , err := bolt .Open ("goblog.db" , 0600 , nil )
19+
20+ if err != nil {
21+ log .Fatal (err )
22+ }
23+ defer db .Close ()
24+
25+ db .Update (func (tx * bolt.Tx ) error {
26+ _ , err := tx .CreateBucketIfNotExists ([]byte ("UsersBucket" ))
27+ if err != nil {
28+ return fmt .Errorf ("Error with UsersBucket: %s" , err )
29+ }
30+ return nil
31+ })
32+
33+ }
34+
1235func LoginPage (w http.ResponseWriter , req * http.Request , _ httprouter.Params ) {
1336 baseT := template .Must (template .New ("base" ).Parse (base ))
1437 baseT = template .Must (baseT .Parse (login ))
@@ -25,10 +48,14 @@ func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params)
2548
2649 if verifyUser (w , req , username , password ) {
2750 http .Redirect (w , req , "/admin/" , http .StatusFound )
51+ } else {
52+ http .Redirect (w , req , "/" , http .StatusFound )
2853 }
2954}
3055
3156func LogoutHandler (w http.ResponseWriter , req * http.Request , p httprouter.Params ) {
57+ delete := http.Cookie {Name : "goblog" , Value : "blah" , Expires : time .Now (), HttpOnly : true , Path : "/" }
58+ http .SetCookie (w , & delete )
3259 http .Redirect (w , req , "/" , http .StatusFound )
3360}
3461
@@ -81,15 +108,45 @@ func AdminPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
81108}
82109
83110func verifyUser (w http.ResponseWriter , r * http.Request , username string , password string ) bool {
84- cookie := http.Cookie {Name : "goblog" , Value : "blah" , Expires : time .Now ().Add (time .Hour * 24 * 7 * 52 ), HttpOnly : true , MaxAge : 50000 , Path : "/" }
85- http .SetCookie (w , & cookie )
86- return true
111+ correctpass := []byte ("" )
112+ db , err := bolt .Open ("goblog.db" , 0600 , nil )
113+ if err != nil {
114+ fmt .Println (err )
115+ }
116+ db .View (func (tx * bolt.Tx ) error {
117+ b := tx .Bucket ([]byte ("UsersBucket" ))
118+ correctpass = b .Get ([]byte (username ))
119+ return nil
120+ })
121+ if password == string (correctpass ) {
122+ cookie := http.Cookie {Name : "goblog" , Value : "blah" , Expires : time .Now ().Add (time .Hour * 24 * 7 * 52 ), HttpOnly : true , MaxAge : 50000 , Path : "/" }
123+ http .SetCookie (w , & cookie )
124+ return true
125+ }
126+ return false
87127}
88128
89129func addUser (username string , password string ) bool {
90- // Insert into database
91-
92- return true
130+ check := []byte ("" )
131+ db , err := bolt .Open ("goblog.db" , 0600 , nil )
132+ db .View (func (tx * bolt.Tx ) error {
133+ b := tx .Bucket ([]byte ("UsersBucket" ))
134+ check = b .Get ([]byte (username )) //username
135+ return nil
136+ })
137+ if err != nil {
138+ fmt .Println (err )
139+ }
140+ if len (check ) > 2 {
141+ db .Update (func (tx * bolt.Tx ) error {
142+ b := tx .Bucket ([]byte ("UsersBucket" ))
143+ err := b .Put ([]byte (username ), []byte (password ))
144+ return err
145+ })
146+ return true
147+ } else {
148+ return false
149+ }
93150}
94151
95152func getUser (w http.ResponseWriter , r * http.Request ) string {
0 commit comments