-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e25aec
commit b4c2bee
Showing
19 changed files
with
299 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ PGDATABASE="userdb" | |
PGHOST="db" | ||
PGPORT=5432 | ||
PGSSLMODE="disable" | ||
SECRET="7RQOLP1d7P" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package server | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/lestrrat-go/jwx/jwa" | ||
"github.com/lestrrat-go/jwx/jwt" | ||
) | ||
|
||
type JWTSign struct { | ||
key interface{} | ||
algorithm jwa.SignatureAlgorithm | ||
} | ||
|
||
func NewJWTToken(name, secret string) (string, error) { | ||
var tokenStr string | ||
js := &JWTSign{key: []byte(secret), algorithm: jwa.HS256} | ||
token := jwt.New() | ||
if err := token.Set(`name`, name); err != nil { | ||
return tokenStr, err | ||
} | ||
payload, err := jwt.Sign(token, js.algorithm, js.key) | ||
if err != nil { | ||
return tokenStr, err | ||
} | ||
tokenStr = string(payload) | ||
return tokenStr, nil | ||
} | ||
|
||
func ParseJWTToken(tokenStr, secret string) error { | ||
_, err := jwt.Parse( | ||
[]byte(tokenStr), | ||
jwt.WithValidate(true), | ||
jwt.WithVerify(jwa.HS256, []byte(secret))) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func authMiddleware(secret string) mux.MiddlewareFunc { | ||
return func(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
authorizationHeader := req.Header.Get("authorization") | ||
bearerToken := strings.Split(authorizationHeader, " ") | ||
if len(bearerToken) != 2 { | ||
log.Println("missing authorization token") | ||
http.Error(w, "forbidden", http.StatusForbidden) | ||
return | ||
} | ||
if err := ParseJWTToken(bearerToken[1], secret); err != nil { | ||
log.Printf("error parsing authorization token %v", err) | ||
http.Error(w, "forbidden", http.StatusForbidden) | ||
return | ||
} | ||
h.ServeHTTP(w, req) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ package server | |
type Config struct { | ||
Port int | ||
DBConfig | ||
Secret string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
cmds/server/migrator/migrations/001_create_users_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.