statping/handlers/oauth.go

55 lines
1.1 KiB
Go
Raw Normal View History

2020-04-07 11:53:32 +00:00
package handlers
import (
"fmt"
"github.com/gorilla/mux"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/users"
"golang.org/x/oauth2"
2020-04-07 11:53:32 +00:00
"net/http"
)
type oAuth struct {
Email string
Username string
*oauth2.Token
2020-04-07 11:53:32 +00:00
}
func oauthHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
provider := vars["provider"]
var err error
var oauth *oAuth
switch provider {
case "google":
oauth, err = googleOAuth(r)
2020-04-07 11:53:32 +00:00
case "github":
oauth, err = githubOAuth(r)
2020-04-20 03:02:34 +00:00
case "slack":
oauth, err = slackOAuth(r)
2020-04-07 11:53:32 +00:00
}
if err != nil {
log.Error(err)
2020-04-20 03:02:34 +00:00
sendErrorJson(err, w, r)
2020-04-07 11:53:32 +00:00
return
}
oauthLogin(oauth, w, r)
}
func oauthLogin(oauth *oAuth, w http.ResponseWriter, r *http.Request) {
user := &users.User{
Id: 0,
Username: oauth.Username,
2020-04-07 11:53:32 +00:00
Email: oauth.Email,
Admin: null.NewNullBool(true),
}
2020-06-26 00:15:59 +00:00
log.Infoln(fmt.Sprintf("OAuth %s User %s logged in from IP %s", oauth.Type, oauth.Email, r.RemoteAddr))
2020-04-07 11:53:32 +00:00
setJwtToken(user, w)
2020-06-26 00:15:59 +00:00
http.Redirect(w, r, core.App.Domain+"/dashboard", http.StatusPermanentRedirect)
2020-04-07 11:53:32 +00:00
}