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

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

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" 包只提供了基本功能。你可以使用它来检查当前用户的电子邮件是否在允许列表中。
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
    }

    var granted bool
    for _, email := range allowed {
        if u.Email == email {
           granted = true
           break;
        }
    }
    if !granted {
        http.Error(w, "你不在允许列表中", 400)
        return
    }
    fmt.Fprintf(w, "你好,%v!", u)
}
英文:

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
    }

     var granted bool
     for _, email := range allowed {
         if u.Email == email {
            granted = true
            break;
         }
     }
     if !granted {
         http.Error(w, "you're not in the allowed list", 400)
         return
     }
     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:

确定