From 70d6860f703d732b93e5938f90871694cfcba22e Mon Sep 17 00:00:00 2001 From: Umputun Date: Thu, 28 Dec 2017 18:42:19 -0600 Subject: [PATCH] safe values for auth --- app/rest/auth/providers.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/app/rest/auth/providers.go b/app/rest/auth/providers.go index 8c62f3de..9129c6a4 100644 --- a/app/rest/auth/providers.go +++ b/app/rest/auth/providers.go @@ -1,6 +1,7 @@ package auth import ( + "fmt" "strings" "golang.org/x/oauth2/facebook" @@ -21,10 +22,10 @@ func NewGoogle(p Params) *Provider { FilesystemStore: p.SessionStore, MapUser: func(data map[string]interface{}) store.User { userInfo := store.User{ - ID: data["email"].(string), - Name: data["name"].(string), - Picture: data["picture"].(string), - Profile: data["profile"].(string), + ID: value(data, "email"), + Name: value(data, "name"), + Picture: value(data, "picture"), + Profile: value(data, "profile"), } if userInfo.Name == "" { userInfo.Name = strings.Split(userInfo.ID, "@")[0] @@ -46,10 +47,10 @@ func NewGithub(p Params) *Provider { FilesystemStore: p.SessionStore, MapUser: func(data map[string]interface{}) store.User { userInfo := store.User{ - ID: data["login"].(string), - Name: data["name"].(string), - Picture: data["avatar_url"].(string), - Profile: data["html_url"].(string), + ID: value(data, "login"), + Name: value(data, "name"), + Picture: value(data, "avatar_url"), + Profile: value(data, "html_url"), } if userInfo.Name == "" { userInfo.Name = userInfo.ID @@ -71,8 +72,8 @@ func NewFacebook(p Params) *Provider { FilesystemStore: p.SessionStore, MapUser: func(data map[string]interface{}) store.User { userInfo := store.User{ - ID: data["id"].(string), - Name: data["name"].(string), + ID: value(data, "id"), + Name: value(data, "name"), // Picture: data["avatar_url"].(string), // Profile: data["html_url"].(string), } @@ -84,3 +85,10 @@ func NewFacebook(p Params) *Provider { }, }) } + +func value(data map[string]interface{}, key string) string { + if val, ok := data[key]; ok { + return fmt.Sprintf("%v", val) + } + return "" +}