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

View File

@ -32,6 +32,17 @@ var Version string = "dev"
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 {
Success bool `json:"success"`
Error string `json:"error"`
@ -47,7 +58,6 @@ func NewFailure(msg string) *failure {
type Server struct {
ClientID string
ClientSecret string
ApiToken string
CookieSecret string
}
@ -56,11 +66,10 @@ func init() {
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{
ClientID: clientId,
ClientSecret: clientSecret,
ApiToken: apiToken,
CookieSecret: cookieSecret,
}
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")
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)
return
}
defer email.Body.Close()
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.Values["authenticated"] = true
session.Values["uname"] = string(data)
session.Values["uname"] = u.Email
if err := session.Save(r, w); err != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}

511
static.go

File diff suppressed because one or more lines are too long