google oauth working along with clean up

cleaned up some things from starz that weren't needed any more.  Also
cleaned up the oauth process and now am storing the users emai but also
have the userInfo struct that gathers all the info that google oauth
userinfo returns
This commit is contained in:
Derek McQuay 2016-08-22 13:03:23 -07:00
parent 901d8e5070
commit 4f7b61cb86
3 changed files with 282 additions and 271 deletions

View File

@ -21,7 +21,6 @@ type Config struct {
Port int Port int
ClientID string ClientID string
ClientSecret string ClientSecret string
ApiToken string
CookieSecret string CookieSecret string
} }
@ -30,7 +29,7 @@ func init() {
} }
func main() { func main() {
var host, clientId, clientSecret, apiToken, cookieSecret string var host, clientId, clientSecret, cookieSecret string
var port int var port int
var run = &cobra.Command{ var run = &cobra.Command{
@ -59,9 +58,6 @@ func main() {
if clientSecret != "" { if clientSecret != "" {
config.ClientSecret = clientSecret config.ClientSecret = clientSecret
} }
if apiToken != "" {
config.ApiToken = apiToken
}
if cookieSecret != "" { if cookieSecret != "" {
config.CookieSecret = cookieSecret config.CookieSecret = cookieSecret
} else { } else {
@ -88,7 +84,6 @@ func main() {
sm, sm,
config.ClientID, config.ClientID,
config.ClientSecret, config.ClientSecret,
config.ApiToken,
config.CookieSecret, config.CookieSecret,
"", "",
) )
@ -146,13 +141,6 @@ func main() {
"", "",
"cookieSecret", "cookieSecret",
) )
run.Flags().StringVarP(
&apiToken,
"apitoken",
"a",
"",
"github apitoken",
)
var rootCmd = &cobra.Command{Use: "app"} var rootCmd = &cobra.Command{Use: "app"}
rootCmd.AddCommand(run) rootCmd.AddCommand(run)

View File

@ -32,6 +32,17 @@ var Version string = "dev"
var start time.Time var start time.Time
type userInfo struct {
Sub string `json:"sub"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Profile string `json:"profile"`
Picture string `json:"picture"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
}
type failure struct { type failure struct {
Success bool `json:"success"` Success bool `json:"success"`
Error string `json:"error"` Error string `json:"error"`
@ -47,7 +58,6 @@ func NewFailure(msg string) *failure {
type Server struct { type Server struct {
ClientID string ClientID string
ClientSecret string ClientSecret string
ApiToken string
CookieSecret string CookieSecret string
} }
@ -56,11 +66,10 @@ func init() {
start = time.Now() start = time.Now()
} }
func NewServer(sm *http.ServeMux, clientId, clientSecret, apiToken, cookieSecret, static string) *Server { func NewServer(sm *http.ServeMux, clientId, clientSecret, cookieSecret, static string) *Server {
server := &Server{ server := &Server{
ClientID: clientId, ClientID: clientId,
ClientSecret: clientSecret, ClientSecret: clientSecret,
ApiToken: apiToken,
CookieSecret: cookieSecret, CookieSecret: cookieSecret,
} }
addRoutes(sm, server, static) addRoutes(sm, server, static)
@ -94,17 +103,24 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
email, err := oauthClient.Get("https://www.googleapis.com/oauth2/v3/userinfo") email, err := oauthClient.Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil { if err != nil {
log.Printf("client.Users.Get() failed with '%s'\n", err) log.Printf("failed with getting userinfo: '%s'\n", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return return
} }
defer email.Body.Close() defer email.Body.Close()
data, _ := ioutil.ReadAll(email.Body) data, _ := ioutil.ReadAll(email.Body)
log.Println("Email body: ", string(data)) u := userInfo{}
err = json.Unmarshal(data, &u)
if err != nil {
log.Printf("failed to unmarshal userinfo: '%s'\n", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
session, _ := store.Get(r, "creds") session, _ := store.Get(r, "creds")
session.Values["authenticated"] = true session.Values["authenticated"] = true
session.Values["uname"] = string(data) session.Values["uname"] = u.Email
if err := session.Save(r, w); err != nil { if err := session.Save(r, w); err != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
} }

511
static.go

File diff suppressed because one or more lines are too long