Auth

    For the security, only authorized clients can invoke services.

    Servers receive requests, first check the auth token. They reject requests with invalid tokens.

    Client must set the token:

    1. func main() {
    2. flag.Parse()
    3. d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
    4. option := client.DefaultOption
    5. option.ReadTimeout = 10 * time.Second
    6. defer xclient.Close()
    7. //xclient.Auth("bearer tGzv3JOkF0XG5Qx2TlKWIA")
    8. xclient.Auth("bearer abcdefg1234567")
    9. args := &example.Args{
    10. A: 10,
    11. B: 20,
    12. }
    13. reply := &example.Reply{}
    14. ctx := context.WithValue(context.Background(), share.ReqMetaDataKey, make(map[string]string))
    15. err := xclient.Call(ctx, "Mul", args, reply)
    16. if err != nil {
    17. log.Fatalf("failed to call: %v", err)
    18. }
    19. log.Printf("%d * %d = %d", args.A, args.B, reply.C)

    By smallnest updated 2018-12-04 11:47:26