Last active
November 25, 2023 06:36
-
-
Save gophertron/016e3822a3bb5e125bb5334557897811 to your computer and use it in GitHub Desktop.
github oauth gorilla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/codegangsta/negroni" | |
gh "github.com/google/go-github/github" | |
"github.com/gorilla/mux" | |
"github.com/gorilla/sessions" | |
"golang.org/x/oauth2" | |
"golang.org/x/oauth2/github" | |
) | |
var Store = sessions.NewCookieStore([]byte("f426e903c4eaf50c96b2473419d3eb70b086bd51775565573958e759192c3eb8")) | |
var ( | |
conf = &oauth2.Config{ | |
ClientID: "<client-id>", | |
ClientSecret: "<client-secret>", | |
Scopes: []string{"user:email"}, | |
Endpoint: github.Endpoint, | |
} | |
) | |
type Handler func(w http.ResponseWriter, r *http.Request) | |
func main() { | |
r := mux.NewRouter().StrictSlash(false) | |
r.HandleFunc("/", GenericHandler("Home Page")) | |
r.HandleFunc("/page1", GenericHandler("Page 1")) | |
r.HandleFunc("/page2", GenericHandler("Page 2")) | |
api := r.PathPrefix("/auth").Subrouter() | |
api.HandleFunc("/login", LoginHandler) | |
api.HandleFunc("/callback", CallbackHandler) | |
api.HandleFunc("/logout", LogoutHandler) | |
mux1 := http.NewServeMux() | |
mux1.Handle("/", negroni.New( | |
negroni.HandlerFunc(AuthMiddleware), | |
negroni.Wrap(r), | |
)) | |
mux1.Handle("/auth/", negroni.New( | |
negroni.HandlerFunc(LoggingMiddleware), | |
negroni.Wrap(r), | |
)) | |
n := negroni.Classic() | |
n.UseHandler(mux1) | |
http.ListenAndServe(":3000", n) | |
} | |
func GenericHandler(s string) Handler { | |
return func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, s) | |
} | |
} | |
func LoginHandler(w http.ResponseWriter, r *http.Request) { | |
url := conf.AuthCodeURL("state", oauth2.AccessTypeOnline) | |
http.Redirect(w, r, url, http.StatusFound) | |
} | |
func LogoutHandler(w http.ResponseWriter, r *http.Request) { | |
session, _ := Store.Get(r, "authtest") | |
delete(session.Values, "user") | |
session.Options.MaxAge = -1 | |
_ = session.Save(r, w) | |
w.WriteHeader(http.StatusCreated) | |
} | |
func CallbackHandler(w http.ResponseWriter, r *http.Request) { | |
code := r.FormValue("code") | |
log.Println("CODE:", code) | |
token, err := conf.Exchange(oauth2.NoContext, code) | |
if err != nil { | |
log.Println("oauthConf.Exchange() failed with", err) | |
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
return | |
} | |
oauthClient := conf.Client(oauth2.NoContext, token) | |
client := gh.NewClient(oauthClient) | |
user, _, err := client.Users.Get("") | |
if err != nil { | |
log.Println("github user fetch failed", err) | |
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
return | |
} | |
session, err := Store.Get(r, "authtest") | |
session.Values["user"] = *user.Login | |
session.Save(r, w) | |
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
} | |
func AuthMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | |
log.Println("AuthMiddleware") | |
session, err := Store.Get(r, "authtest") | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
log.Println(session.Values["user"]) | |
if session.Values["user"] == nil { | |
http.Redirect(w, r, "/auth/login", 301) | |
} else { | |
next(w, r) | |
} | |
} | |
func LoggingMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | |
log.Println("LoggingMiddleware") | |
next(w, r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment