Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "log"
  5. "math/rand"
  6. "time"
  7.  
  8. "github.com/google/uuid"
  9. )
  10.  
  11. var token string
  12.  
  13. func main() {
  14. log.Println("Starting token worker")
  15. tokenReceiver := make(chan string)
  16. go tokenWorker(tokenReceiver)
  17. for {
  18. select {
  19. case val := <-tokenReceiver:
  20. log.Println("Received new AccessToken:", val)
  21. token = val
  22. }
  23. }
  24. }
  25.  
  26. func tokenWorker(tokenReceiver chan string) {
  27. at := GetAccessToken()
  28. log.Println("Sending new token to receiver")
  29. tokenReceiver <- at.Token
  30. log.Println("Sleeping for", at.ExpiresIn)
  31. time.Sleep(at.ExpiresIn)
  32. go tokenWorker(tokenReceiver)
  33. }
  34.  
  35. // GetAccessToken creates a stub AccessToken
  36. func GetAccessToken() *AccessToken {
  37. u, _ := uuid.NewRandom()
  38. return &AccessToken{
  39. Token: u.String(),
  40. ExpiresIn: time.Duration(rand.Int63n(5)) * time.Second,
  41. }
  42. }
  43.  
  44. // AccessToken represents a token that can be used to authenticate with a REST API
  45. type AccessToken struct {
  46. Token string
  47. ExpiresIn time.Duration
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement