Remove Redis from microsubHandler and pass as parameter
All checks were successful
the build was successful
All checks were successful
the build was successful
This commit is contained in:
parent
e02050013d
commit
89a50821d4
|
|
@ -39,7 +39,7 @@ type TokenResponse struct {
|
||||||
|
|
||||||
var authHeaderRegex = regexp.MustCompile("^Bearer (.+)$")
|
var authHeaderRegex = regexp.MustCompile("^Bearer (.+)$")
|
||||||
|
|
||||||
func (h *microsubHandler) cachedCheckAuthToken(header string, r *TokenResponse) bool {
|
func (h *microsubHandler) cachedCheckAuthToken(conn redis.Conn, header string, r *TokenResponse) bool {
|
||||||
log.Println("Cached checking Auth Token")
|
log.Println("Cached checking Auth Token")
|
||||||
|
|
||||||
tokens := authHeaderRegex.FindStringSubmatch(header)
|
tokens := authHeaderRegex.FindStringSubmatch(header)
|
||||||
|
|
@ -51,7 +51,7 @@ func (h *microsubHandler) cachedCheckAuthToken(header string, r *TokenResponse)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
values, err := redis.Values(h.Redis.Do("HGETALL", key))
|
values, err := redis.Values(conn.Do("HGETALL", key))
|
||||||
if err == nil && len(values) > 0 {
|
if err == nil && len(values) > 0 {
|
||||||
if err = redis.ScanStruct(values, r); err == nil {
|
if err = redis.ScanStruct(values, r); err == nil {
|
||||||
return true
|
return true
|
||||||
|
|
@ -65,16 +65,16 @@ func (h *microsubHandler) cachedCheckAuthToken(header string, r *TokenResponse)
|
||||||
|
|
||||||
if authorized {
|
if authorized {
|
||||||
fmt.Printf("Token response: %#v\n", r)
|
fmt.Printf("Token response: %#v\n", r)
|
||||||
_, err = h.Redis.Do("HMSET", redis.Args{}.Add(key).AddFlat(r)...)
|
_, err = conn.Do("HMSET", redis.Args{}.Add(key).AddFlat(r)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while setting token: %v\n", err)
|
log.Printf("Error while setting token: %v\n", err)
|
||||||
return authorized
|
return authorized
|
||||||
}
|
}
|
||||||
_, err = h.Redis.Do("EXPIRE", key, uint64(10*time.Minute/time.Second))
|
_, err = conn.Do("EXPIRE", key, uint64(10*time.Minute/time.Second))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while setting expire on token: %v\n", err)
|
log.Printf("Error while setting expire on token: %v\n", err)
|
||||||
log.Println("Deleting token")
|
log.Println("Deleting token")
|
||||||
_, err = h.Redis.Do("DEL", key)
|
_, err = conn.Do("DEL", key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Deleting token failed: %v", err)
|
log.Printf("Deleting token failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,6 @@ func main() {
|
||||||
http.Handle("/microsub", µsubHandler{
|
http.Handle("/microsub", µsubHandler{
|
||||||
Backend: backend,
|
Backend: backend,
|
||||||
HubIncomingBackend: &hubBackend,
|
HubIncomingBackend: &hubBackend,
|
||||||
Redis: nil,
|
|
||||||
})
|
})
|
||||||
http.Handle("/incoming/", &incomingHandler{
|
http.Handle("/incoming/", &incomingHandler{
|
||||||
Backend: &hubBackend,
|
Backend: &hubBackend,
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ import (
|
||||||
type microsubHandler struct {
|
type microsubHandler struct {
|
||||||
Backend microsub.Microsub
|
Backend microsub.Microsub
|
||||||
HubIncomingBackend HubBackend
|
HubIncomingBackend HubBackend
|
||||||
Redis redis.Conn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -57,7 +56,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var token TokenResponse
|
var token TokenResponse
|
||||||
|
|
||||||
if !h.cachedCheckAuthToken(authorization, &token) {
|
if !h.cachedCheckAuthToken(conn, authorization, &token) {
|
||||||
log.Printf("Token could not be validated")
|
log.Printf("Token could not be validated")
|
||||||
http.Error(w, "Can't validate token", 403)
|
http.Error(w, "Can't validate token", 403)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user