How can I manage accounts for my google app engine (with golang)?

huangapple go评论112阅读模式
英文:

How can I manage accounts for my google app engine (with golang)?

问题

我阅读了一个名为“使用用户服务”的文档,它很有用。
但是我想要只允许几个用户访问我的GAE,并限制其他人。

那么,我该如何管理我的Google应用引擎(使用golang)的用户账户?

我将使用“Google账户”系统。

我需要你的帮助。
谢谢!祝你有美好的一天~

英文:

I read a document "Using the Users Service" and it works.
But I want to allow only several users to access my GAE, and restrict others.

So, How can I manage user accounts for my google app engine (with golang)?

I will use "Google account" system.

I need your help.
Thank you! Have a nice day~

答案1

得分: 0

我认为你有两个选择:

  1. 你可以限制只允许你的 Google App 域的用户访问,方法是进入 Administration >> Application Settings >> Authentication Type。
  2. "appengine/user" 包只提供了基本功能。你可以使用它来检查当前用户的电子邮件是否在允许列表中。
  1. var allowed = []string{"tom@example.com", "jack@example.com"}
  2. func handler(w http.ResponseWriter, r *http.Request) {
  3. c := appengine.NewContext(r)
  4. u := user.Current(c)
  5. if u == nil {
  6. url, err := user.LoginURL(c, r.URL.String())
  7. if err != nil {
  8. http.Error(w, err.Error(), http.StatusInternalServerError)
  9. return
  10. }
  11. w.Header().Set("Location", url)
  12. w.WriteHeader(http.StatusFound)
  13. return
  14. }
  15. var granted bool
  16. for _, email := range allowed {
  17. if u.Email == email {
  18. granted = true
  19. break;
  20. }
  21. }
  22. if !granted {
  23. http.Error(w, "你不在允许列表中", 400)
  24. return
  25. }
  26. fmt.Fprintf(w, "你好,%v!", u)
  27. }
英文:

I think you have 2 options:

  1. You can limit only the user of your Google App domain, go to Administration >> Application Settings >> Authentication Type.

  2. The "appengine/user" pakage just give you the basic function. You can use it to check if the current user email is in the allowed-list or not.

    var allowed = []string{"tom@example.com", "jack@example.com"}
    func handler(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    u := user.Current(c)
    if u == nil {
    url, err := user.LoginURL(c, r.URL.String())
    if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
    }
    w.Header().Set("Location", url)
    w.WriteHeader(http.StatusFound)
    return
    }

    1. var granted bool
    2. for _, email := range allowed {
    3. if u.Email == email {
    4. granted = true
    5. break;
    6. }
    7. }
    8. if !granted {
    9. http.Error(w, "you're not in the allowed list", 400)
    10. return
    11. }
    12. fmt.Fprintf(w, "Hello, %v!", u)

    }

huangapple
  • 本文由 发表于 2013年11月11日 12:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/19898823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定