Error handling on generate handler
This commit is contained in:
		
							parent
							
								
									61507766fe
								
							
						
					
					
						commit
						26313ee041
					
				
					 3 changed files with 77 additions and 58 deletions
				
			
		|  | @ -45,7 +45,7 @@ func main() { | ||||||
| 		panic(err) | 		panic(err) | ||||||
| 	} | 	} | ||||||
| 	protect := http.HandlerFunc(protectMe) | 	protect := http.HandlerFunc(protectMe) | ||||||
| 	http.HandleFunc("/authenticate", j.GenerateToken) | 	http.Handle("/authenticate", j.GenerateToken()) | ||||||
| 	http.Handle("/secure", j.Secure(protect, verifyClaimsFunc)) | 	http.Handle("/secure", j.Secure(protect, verifyClaimsFunc)) | ||||||
| 	http.ListenAndServe(":8080", nil) | 	http.ListenAndServe(":8080", nil) | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										131
									
								
								jwt.go
									
										
									
									
									
								
							
							
						
						
									
										131
									
								
								jwt.go
									
										
									
									
									
								
							|  | @ -162,64 +162,83 @@ func (m *JWTMiddleware) Secure(h http.Handler, v VerifyClaimsFunc) http.Handler | ||||||
| 	return errorHandler(secureHandler) | 	return errorHandler(secureHandler) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (m *JWTMiddleware) GenerateToken(w http.ResponseWriter, r *http.Request) { | func (m *JWTMiddleware) GenerateToken() http.Handler { | ||||||
| 	var b map[string]string | 	generateHandler := func(w http.ResponseWriter, r *http.Request) *jwtError { | ||||||
| 	err := json.NewDecoder(r.Body).Decode(&b) | 		var b map[string]string | ||||||
| 	if err != nil { | 		err := json.NewDecoder(r.Body).Decode(&b) | ||||||
| 		log.Printf("error (%v) while parsing authorization", err) | 		if err != nil { | ||||||
| 		http.Error(w, ErrParsingCredentials.Error(), http.StatusInternalServerError) | 			return &jwtError{ | ||||||
| 		return | 				status:  http.StatusInternalServerError, | ||||||
| 	} | 				err:     ErrParsingCredentials, | ||||||
| 	err = m.auth(b["email"], b["password"]) | 				message: "parsing authorization", | ||||||
| 	if err != nil { | 			} | ||||||
| 		log.Printf("error (%v) while performing authorization", err) | 		} | ||||||
| 		http.Error(w, err.Error(), http.StatusInternalServerError) | 		err = m.auth(b["email"], b["password"]) | ||||||
| 		return | 		if err != nil { | ||||||
|  | 			return &jwtError{ | ||||||
|  | 				status:  http.StatusInternalServerError, | ||||||
|  | 				err:     err, | ||||||
|  | 				message: "performing authorization", | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// For now, the header will be static | ||||||
|  | 		header, err := encode(fmt.Sprintf(`{"typ":%q,"alg":%q}`, typ, alg)) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &jwtError{ | ||||||
|  | 				status:  http.StatusInternalServerError, | ||||||
|  | 				err:     ErrEncoding, | ||||||
|  | 				message: "encoding header", | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// Generate claims for user | ||||||
|  | 		claims, err := m.claims(b["email"]) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &jwtError{ | ||||||
|  | 				status:  http.StatusInternalServerError, | ||||||
|  | 				err:     err, | ||||||
|  | 				message: "generating claims", | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		claimsJson, err := json.Marshal(claims) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &jwtError{ | ||||||
|  | 				status:  http.StatusInternalServerError, | ||||||
|  | 				err:     ErrEncoding, | ||||||
|  | 				message: "marshalling claims", | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		claimsSet, err := encode(claimsJson) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &jwtError{ | ||||||
|  | 				status:  http.StatusInternalServerError, | ||||||
|  | 				err:     ErrEncoding, | ||||||
|  | 				message: "encoding claims", | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		toSig := strings.Join([]string{header, claimsSet}, ".") | ||||||
|  | 
 | ||||||
|  | 		h := hmac.New(sha256.New, []byte(m.secret)) | ||||||
|  | 		h.Write([]byte(toSig)) | ||||||
|  | 		sig, err := encode(h.Sum(nil)) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &jwtError{ | ||||||
|  | 				status:  http.StatusInternalServerError, | ||||||
|  | 				err:     ErrEncoding, | ||||||
|  | 				message: "encoding signature", | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		response := strings.Join([]string{toSig, sig}, ".") | ||||||
|  | 		w.Write([]byte(response)) | ||||||
|  | 		return nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// For now, the header will be static | 	return errorHandler(generateHandler) | ||||||
| 	header, err := encode(fmt.Sprintf(`{"typ":%q,"alg":%q}`, typ, alg)) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Printf("error (%v) while encoding header", err) |  | ||||||
| 		http.Error(w, ErrEncoding.Error(), http.StatusInternalServerError) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// Generate claims for user |  | ||||||
| 	claims, err := m.claims(b["email"]) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Printf("error (%v) while generating claims", err) |  | ||||||
| 		http.Error(w, err.Error(), http.StatusInternalServerError) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	claimsJson, err := json.Marshal(claims) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Printf("error (%v) while marshalling claims") |  | ||||||
| 		http.Error(w, ErrEncoding.Error(), http.StatusInternalServerError) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	claimsSet, err := encode(claimsJson) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Printf("error (%v) while encoding claims") |  | ||||||
| 		http.Error(w, ErrEncoding.Error(), http.StatusInternalServerError) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	toSig := strings.Join([]string{header, claimsSet}, ".") |  | ||||||
| 
 |  | ||||||
| 	h := hmac.New(sha256.New, []byte(m.secret)) |  | ||||||
| 	h.Write([]byte(toSig)) |  | ||||||
| 	sig, err := encode(h.Sum(nil)) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Printf("error (%v) while encoding signature") |  | ||||||
| 		http.Error(w, ErrEncoding.Error(), http.StatusInternalServerError) |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	response := strings.Join([]string{toSig, sig}, ".") |  | ||||||
| 	w.Write([]byte(response)) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func encode(s interface{}) (string, error) { | func encode(s interface{}) (string, error) { | ||||||
|  |  | ||||||
|  | @ -72,7 +72,7 @@ func newToken(t *testing.T) (string, *JWTMiddleware) { | ||||||
| 		t.Error(err) | 		t.Error(err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	ts := httptest.NewServer(http.HandlerFunc(middleware.GenerateToken)) | 	ts := httptest.NewServer(middleware.GenerateToken()) | ||||||
| 	defer ts.Close() | 	defer ts.Close() | ||||||
| 
 | 
 | ||||||
| 	resp, err := http.Post(ts.URL, "application/json", bytes.NewReader(body)) | 	resp, err := http.Post(ts.URL, "application/json", bytes.NewReader(body)) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon