项目作者: tdely

项目描述 :
Quick and easy HTTP requests with Hawk authentication for Go.
高级语言: Go
项目地址: git://github.com/tdely/go-hawk.git
创建时间: 2019-04-12T21:03:19Z
项目社区:https://github.com/tdely/go-hawk

开源协议:MIT License

下载


go-hawk

godoc License MIT Build Status Coverage Report Coverage Report

Package hawk provides a quick and easy way to send HTTP requests with
Hawk authentication.

Installation

  1. go get -u gitlab.com/triumvir/nxal

Getting Started

Easiest is to use the provided client:

  1. import (
  2. "crypto"
  3. hawk "gitlab.com/tdely/go-hawk"
  4. "net/http"
  5. "strings"
  6. )
  7. c := &http.Client{}
  8. hc := hawk.NewClient("your-hawk-id", []byte("secret"), crypto.SHA256, 6)
  9. body := strings.NewReader("Hello world!")
  10. req, err := hc.NewRequest("POST", "https://example.com/greeting", body, "text/plain", "some-app-ext-data")
  11. resp, err := c.Do(req)
  12. // Check validity of response
  13. valid := hc.ValidateResponse(*resp)

But if you want to not do payload verification or want to make life harder:

  1. import (
  2. "crypto"
  3. hawk "gitlab.com/tdely/go-hawk"
  4. "net/http"
  5. "strings"
  6. "time"
  7. )
  8. c := &http.Client{}
  9. body := strings.NewReader("Hello world!")
  10. req, _ := http.NewRequest("POST", "https://example.com/greeting", body)
  11. hd := hawk.Details{
  12. Algorithm: crypto.SHA256,
  13. Host: "example.com",
  14. Port: "443",
  15. URI: "/greeting",
  16. ContentType: "plain/text",
  17. Content: []byte("Hello world!"),
  18. Method: "POST",
  19. Ext: "some-app-ext-data"}
  20. hd.Timestamp = time.Now().Unix()
  21. hd.Nonce = hawk.NewNonce(6)
  22. h, _ := hd.Create()
  23. // h.Validate()
  24. h.Finalize([]("secret"))
  25. auth := h.GetAuthorization("your-hawk-id")
  26. req.Header.Add("Content-Type", "plain/text")
  27. req.Header.Add("Authorization", auth)
  28. resp, err := c.Do(req)
  29. // valid := h.ValidateResponse([]byte("justtesting"), *resp)