本文整理汇总了Golang中github.com/stretchr/objx.MustFromURLQuery函数的典型用法代码示例。如果您正苦于以下问题:Golang MustFromURLQuery函数的具体用法?Golang MustFromURLQuery怎么用?Golang MustFromURLQuery使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MustFromURLQuery函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: callbackHandler
func callbackHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
providerName := kami.Param(ctx, "provider")
provider, err := gomniauth.Provider(providerName)
if err != nil {
log.Fatalln("認証プロバイダーの取得に失敗しました", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("認証を完了できませんでした", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("ユーザーの取得に失敗しました", provider, "- ", err)
}
authCookieValue := objx.New(map[string]interface{}{
"name": user.Name(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
log.Println("######### ", authCookieValue)
w.Header()["Location"] = []string{"/"}
w.WriteHeader(http.StatusTemporaryRedirect)
}
开发者ID:kyokomi-sandbox,项目名称:sandbox,代码行数:29,代码来源:gomniauth.go
示例2: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error while getting provider:", provider, " : ", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error while getting GetBeginAuthURL for", provider, " : ", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error while getting provider:", provider, " : ", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error while completing auth for", provider, " : ", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error while getting user for", provider)
}
// User Id is a md5 of the name
m := md5.New()
io.WriteString(m, strings.ToLower(user.Email()))
userId := fmt.Sprintf("%x", m.Sum(nil))
// save some data
authCookieValue := objx.New(map[string]interface{}{
"userid": userId,
"name": user.Name(),
"avatar_url": user.AvatarURL(),
"email": user.Email(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:oyvindsk,项目名称:go-blueprints-chat,代码行数:60,代码来源:auth.go
示例3: loginHandler
//loginHandler handle the third party login process
//format: /auth/{action}/{provider}
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
if len(segs) != 4 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Invalid URL path %s", r.URL.Path)
return
}
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
loginURL, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error when trying to GetBeginAuthURL for", provider, "-", err)
}
w.Header().Set("Location", loginURL)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to complete auth for", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user creds for", provider, "-", err)
}
chatUser := &chatUser{User: user}
m := md5.New()
io.WriteString(m, strings.ToLower(user.Email()))
userid := fmt.Sprintf("%x", m.Sum(nil))
chatUser.uniqueID = userid
avatarURL, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("Error when trying to getAvatarURL", "-", err)
}
authCookieValue := objx.New(map[string]interface{}{
"userID": userid,
"name": user.Name(),
"avatar_url": avatarURL,
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:onufert,项目名称:chat,代码行数:61,代码来源:auth.go
示例4: loginHandler
// loginHandlerはサードパーティーへのログインの処理を受け持ちます
// パスの形式: /auth/{action}/{provider}
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("認証プロバイダーの取得に失敗しました:", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("GetBeginAuthURL の呼び出し中にエラーが発生しました:", provider, "-", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("認証プロバイダーの取得に失敗しました", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("認証を完了できませんでした", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("ユーザーの取得に失敗しました", provider, "-", err)
}
chatUser := &chatUser{User: user}
m := md5.New()
io.WriteString(m, strings.ToLower(user.Name()))
chatUser.uniqueID = fmt.Sprintf("%x", m.Sum(nil))
avatarURL, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("GetAvatarURLに失敗しました", "-", err)
}
// データを保存します
authCookieValue := objx.New(map[string]interface{}{
"userid": chatUser.uniqueID,
"name": user.Name(),
"avatar_url": avatarURL,
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "アクション%sには非対応です", action)
}
}
开发者ID:masutaka,项目名称:goblueprints,代码行数:61,代码来源:auth.go
示例5: loginHandler
// loginHandler handles the third-party login process.
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error when trying to GetBeginAuthURL for", provider, "-", err)
}
w.Header()["Location"] = []string{loginUrl}
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
// get the credentials
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to complete auth for", provider, "-", err)
}
// get the user
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user from", provider, "-", err)
}
// save some data
authCookieValue := objx.New(map[string]interface{}{
"name": user.Name(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header().Set("Location", "/chat")
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:0-T-0,项目名称:goblueprints,代码行数:57,代码来源:auth.go
示例6: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatal("Failed autorizatoin: ", provider, "-", err)
}
loginURL, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Had error during calling GetBeginAuthURL:", provider, "-", err)
}
w.Header().Set("Location", loginURL)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("failed to get authorization from auth provider", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Could not finish authorization", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("failed get user", provider, "-", err)
}
authCookieValue := objx.New(map[string]interface{}{
"name": user.Name(),
"avatar_url": user.AvatarURL(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "can't handle action: %s", action)
}
}
开发者ID:TakaakiFuruse,项目名称:go_practice,代码行数:54,代码来源:auth.go
示例7: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
action := ps.ByName("action")
provider := ps.ByName("provider")
s := sessions.GetSession(r)
switch action {
case "login":
// gomniauth Provider의 login 페이지로 이동
p, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln(err)
}
loginUrl, err := p.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln(err)
}
http.Redirect(w, r, loginUrl, http.StatusFound)
case "callback":
// gomniauth 콜백 처리
p, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln(err)
}
creds, err := p.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln(err)
}
// 콜백 결과로 부터 사용자 정보 확인
user, err := p.GetUser(creds)
if err != nil {
log.Fatalln(err)
}
if err != nil {
log.Fatalln(err)
}
u := &User{
Uid: user.Data().Get("id").MustStr(),
Name: user.Name(),
Email: user.Email(),
AvatarUrl: user.AvatarURL(),
}
SetCurrentUser(r, u) // 사용자 정보를 세션에 저장
http.Redirect(w, r, s.Get(nextPageKey).(string), http.StatusFound)
default:
http.Error(w, "Auth action '"+action+"' is not supported", http.StatusNotFound)
}
}
开发者ID:RorNHJ,项目名称:GoLanguage,代码行数:53,代码来源:auth.go
示例8: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("認証プロバイダーの取得に失敗しました:", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("GetBeginAuthURLの呼び出し中にエラーが発生しました", provider, "-", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("認証プロバイダーの取得に失敗しました。", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("認証を完了できませんでした", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("ユーザーの取得に失敗しました", provider, "-", err)
}
authCookieValue := objx.New(map[string]interface{}{
"name": user.Name(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header().Set("Location", "/chat")
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "アクション%sには非対応です", action)
}
}
开发者ID:mosson,项目名称:chat-sample,代码行数:51,代码来源:auth.go
示例9: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Failed to get authentication provider from ", provider, " - ", err)
}
loginURL, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Failed in GetBeginAuthURL for ", provider, " - ", err)
}
w.Header().Set("Location", loginURL)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Failed to get authentication provider from ", provider, " - ", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Failed in authentication for ", provider, " - ", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Failed to get user from ", provider, " - ", err)
}
authCookieValue := objx.New(map[string]interface{}{
"name": user.Name(),
"email": user.Email(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "action %s is not supported", action)
}
}
开发者ID:feeblefakie,项目名称:misc,代码行数:51,代码来源:auth.go
示例10: loginHandler
func loginHandler(w http.ReponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("fail to get provider", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("error GetBeginAuthURL", provider, "-", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("fail to get provider", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("error CompleteAuth", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("error GetUser", provider, "-", err)
}
authCookieValue := objx.New(map[string]interface{}{
"name": user.Name(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string("/2/1")
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "not accept %s", action)
}
}
开发者ID:ntk1000,项目名称:go-web-book-playground,代码行数:45,代码来源:auth.go
示例11: loginHander
// loginHander handles the third-party login process.
// format: /auth/{action}/{provider}
func loginHander(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
if len(segs) < 4 {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "format: /auth/{action}/{provider}")
return
}
action := segs[2]
provider := segs[3]
switch action {
case "login":
// redirect users to the provider's authentication page
// when they land on our /auth/login/{provider} path.
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
loginURL, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error when trying to GetBeginAuthURL for", provider, "-", err)
}
w.Header().Set("Location", loginURL)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
// the provider rediect back to our callback endpoint, the
// Gomniauth will exchange the grant code for an access token.
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
// parse the RawQuery from the http.Request into objx.Map and the
// CompleteAuth method uses the URL query parameter values to
// complete the authentication handshake with the provider.
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to complete auth for", provider, "-", err)
}
// use the specified credential to access the basic user info.
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user from", provider, "-", err)
}
chatUser := &chatUser{User: user}
// create a new md5 hasher, because the hasher implements
// the io.Writer interface, we can use io.WriteString to
// write a string of bytes to it. Calling Sum returns the
// current hash for the bytes written.
m := md5.New()
io.WriteString(m, strings.ToLower(user.Email()))
chatUser.uniqueID = fmt.Sprintf("%x", m.Sum(nil))
avatarURL, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("Error when trying to GetAvatarURL", "-", err)
}
// save the auth info into cookie
authCookieValue := objx.New(map[string]interface{}{
"userid": chatUser.uniqueID,
"name": user.Name(),
"avatar_url": avatarURL,
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
// write out an error message and return an
// http.StatusNotFound status code (404)
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:liyu-wang,项目名称:go-chat,代码行数:81,代码来源:auth.go
示例12: loginHander
func loginHander(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error when trying to GetBeginAuthURL for", provider, "-", err)
}
w.Header()["Location"] = []string{loginUrl}
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
//get the credentials
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
//get the user
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user from", provider, "-", err)
}
chatUser := &chatUser{User: user}
m := md5.New()
io.WriteString(m, strings.ToLower(user.Name()))
chatUser.uniqueID = fmt.Sprintf("%x", m.Sum(nil))
avatarURL, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("Error when trying to GetAvatarURL", "-", err)
}
//save data
authCookieValue := objx.New(map[string]interface{}{
"userid": chatUser.uniqueID,
"name": user.Name(),
"avatar_url": avatarURL,
"email": user.Email(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header().Set("Location", "/chat")
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Auth actions %s no supported", action)
}
}
开发者ID:anand180,项目名称:go-blueprints,代码行数:68,代码来源:auth.go
示例13: loginHandler
// loginHandler handles the third-party login process.
// 1.ユーザーはプロバイダーを選択する
// 2.プロバイダーのサイトにクライアントアプリケーションのIDと共にリダイレクトされる
// 3.ユーザーはクライアントアプリケーションからのデータへのアクセスを許可
// 4.ユーザーは認可コードと共にクライアントアプリケーションにリダイレクト
// 5.クライアントアプリケーションはプロバイダーに認可コードを送信する
// 6.プロバイダーからクライアントアプリケーションにアクセストークンが送信される
// 7.アクセストークンを使用して、ユーザーの情報にアクセスする
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
// func (provider *SoundcloudProvider) GetBeginAuthURL(state *common.State, options objx.Map) (string, error) {...}
// 認証プロセスが開始される前にリファラーにリダイレクトする場合は第一引数を指定する
// 認証プロバイダーから追加情報を取得したい場合は第二引数を指定する
loginURL, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error when trying to GetBeginAuthURL for", provider, "-", err)
}
w.Header()["Location"] = []string{loginURL}
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
// get the credentials
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to complete auth for", provider, "-", err)
}
// get the user
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user from", provider, "-", err)
}
chatUser := &chatUser{User: user}
m := md5.New()
io.WriteString(m, strings.ToLower(user.Name()))
chatUser.uniqueID = fmt.Sprintf("%x", m.Sum(nil))
avatarURL, err := avatars.URL(chatUser)
// クッキーにキャッシュされる
authCookieValue := objx.New(map[string]interface{}{
"userId": chatUser.uniqueID,
"name": user.Name(),
"avatar_url": avatarURL,
"email": user.Email(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header().Set("Location", "/chat")
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:keiwt,项目名称:goblueprints,代码行数:72,代码来源:auth.go
示例14: loginHandler
// loginHandler handles the third-party login process.
// format: /auth/{action}/{provider}
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
// get the provider object that matches the object specfied in the URL (such as google or github)
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatal("Error when trying to get provider", provider, "-", err)
}
// get the location where we must send users in order to start the authentication process
// GetBeginAuthURL
// first argument:
// is a state map of data that is encoded, and signed and sent to the authentication provider.
// The provider doesn't do anything with the state, it just sends it back to our callback endpoint.
// This is useful if, for example, we want to redirect the user back to the original page they were
// trying to access before the authentication process intervened. For our purpose, we have only the
// /chat endpoint, so we don't need to worry about sending any state.
// second argument:
// is a map of additional options that will be sent to the authentication provider,
// which somehow modi es the behavior of the authentication process.
// For example, you can specify your own scope parameter, which allows you to make a request
// for permission to access additional information from the provider. For more information
// about the available options, search for OAuth2 on the Internet or read the documentation
// for each provider, as these values differ from service to service.
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("Error when trying to GetBeginAuthURL for", provider, "-", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
// get the provider
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
// exchanging it for an access token as per the OAuth specification
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to complete auth for", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user from", provider, "-", err)
}
m := md5.New()
io.WriteString(m, strings.ToLower(user.Name()))
userId := fmt.Sprintf("%x", m.Sum(nil))
// save some data
authCookieValue := objx.New(map[string]interface{}{
"userid": userId,
"name": user.Name(),
"avatar_url": user.AvatarURL(),
"email": user.Email(),
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:rabbitcount,项目名称:goblueprints,代码行数:75,代码来源:auth.go
示例15: loginHandler
//地址类型 /auth/{action}/{provider}
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login": //登入
log.Println("TODO: 登入处理", provider)
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("认证获取失败", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("GetBeginAuthUrl获取失败", provider, "-", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback": //认证成功返回信息解析
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("认证获取失败", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("认证中断未完成", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("用户信息获取失败", provider, "-", err)
}
chatUser := &chatUser{User: user}
m := md5.New()
io.WriteString(m, strings.ToLower(user.Name()))
chatUser.uniqueID = fmt.Sprintf("%x", m.Sum(nil))
avatarURL, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("GetAvatarURL获取失败", "-", err)
}
authCookieValue := objx.New(map[string]interface{}{
"userid": chatUser.uniqueID,
"name": user.Name(),
"avatar_url": avatarURL,
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "操作%s非对应", action)
}
}
开发者ID:oywc410,项目名称:MYPG,代码行数:62,代码来源:auth.go
示例16: loginHandler
// loginHandler handles the third-party login process
// format: /auth/{action}/{provider}
// callback: /auth/callback/{provider}
func loginHandler(w http.ResponseWriter, r *http.Request) {
log.Println("req url path:", r.URL.Path)
segs := strings.Split(r.URL.Path, "/")
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("error when trying to get provider", provider, "-", err)
}
loginUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("error when trying to get GetBeginAuthURL for", provider, "-", err)
}
w.Header().Set("Location", loginUrl)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("error when trying to get provider", provider, "-", err)
}
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("error when trying to complete auth for", provider, "-", err)
}
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("error when trying to get user from", provider, "-", err)
}
// log.Println("user name:", user.Name())
// log.Println("user avatar url:", user.AvatarURL())
m := md5.New()
io.WriteString(m, strings.ToLower(user.Name()))
chatUser := &chatUser{User: user}
chatUser.uniqueId = fmt.Sprintf("%x", m.Sum(nil))
avatarUrl, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("error when trying to GetAvatarURL", "-", err)
}
// save some data
authCookieValue := objx.New(map[string]interface{}{
"userid": chatUser.UniqueId(),
"name": user.Name(),
"avatar_url": avatarUrl,
}).MustBase64()
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/",
})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
// todo: use /auth/nonsense ?
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Auth action %s not supported", action)
}
}
开发者ID:hongjianzhu,项目名称:go-chat,代码行数:74,代码来源:auth.go
示例17: loginHandler
// loginHandler handles the third-party login process.
// format: /auth/{action}/{provider}
func loginHandler(w http.ResponseWriter, r *http.Request) {
segs := strings.Split(r.URL.Path, "/")
// TODO: handle exception if there are too few segments
action := segs[2]
provider := segs[3]
switch action {
case "login":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Eerror when trying to get provider", provider, "-", err)
}
loginURL, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
log.Fatalln("error when trying to GetBeginAuthURL for", provider, "-", err)
}
// Since http.ResponseWriter is an interface and header is a method on that
// interface, we have to call it so it will return an http.Header
// instead of w.Header.Set(...) we use w.Header().Set(...)
w.Header().Set("Location", loginURL)
w.WriteHeader(http.StatusTemporaryRedirect)
case "callback":
provider, err := gomniauth.Provider(provider)
if err != nil {
log.Fatalln("Error when trying to get provider", provider, "-", err)
}
// Parsin RawQuery from http.Request into objx.Map (multi-purpose map type
// that gomniauth uses) and the CompleteAuth method uses the URL query param
// values to complete the authentication handshake. If all is okay - creds
// are given
creds, err := provider.CompleteAuth(objx.MustFromURLQuery(r.URL.RawQuery))
if err != nil {
log.Fatalln("Error when trying to complete auth for", provider, "-", err)
}
// now accessing user's basic data
user, err := provider.GetUser(creds)
if err != nil {
log.Fatalln("Error when trying to get user from", provider, "-", err)
}
chatUser := &chatUser{User: user}
fmt.Println("User authenticated: ", user.Name())
// creating new md5 hasher from crypto package, implements io.Writer interface
m := md5.New()
// ensuring email is lower case and generate md5 hash
// writing a string of bytes to hasher through io.WriteString
io.WriteString(m, strings.ToLower(user.Name()))
// caling Sum on hasher returns the current hash for the bytes written
chatUser.uniqueID = fmt.Sprintf("%x", m.Sum(nil))
avatarURL, err := avatars.GetAvatarURL(chatUser)
if err != nil {
log.Fatalln("Error when trying to GetAvatarURL", "-", err)
}
// encoding user data with Base64 in JSON object
authCookieValue := objx.New(map[string]interface{}{
"userid": chatUser.uniqueID,
"name": user.Name(),
// adding avatar URL to cookie
"avatar_url": avatarURL,
"email": user.Email(),
}).MustBase64()
// storing encoded object in cookie
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: authCookieValue,
Path: "/"})
w.Header()["Location"] = []string{"/chat"}
w.WriteHeader(http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "auth action %s not supported", action)
}
}
开发者ID:rusenask,项目名称:chat,代码行数:78,代码来源:auth.go
注:本文中的github.com/stretchr/objx.MustFromURLQuery函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论