-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"database/sql" | ||
"embed" | ||
|
@@ -14,21 +15,150 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/k0kubun/pp/v3" | ||
_ "modernc.org/sqlite" | ||
) | ||
|
||
func hello(s AuthProvider) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Println("/hello") | ||
type Router struct { | ||
db *sql.DB | ||
authProvider AuthProvider | ||
jwtSecret []byte | ||
} | ||
|
||
const AUTH_COOKIE_NAME string = "csc-auth" | ||
|
||
func (r *Router) signin(w http.ResponseWriter, req *http.Request) { | ||
attributes := r.authProvider.attributesFromContext(req.Context()) | ||
|
||
pp.Println(attributes) | ||
now := time.Now() | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"idm_id": attributes.IDMUID, | ||
"iat": now.Unix(), | ||
"exp": now.AddDate(1, 0, 0).Unix(), | ||
}) | ||
|
||
signedTokenString, err := token.SignedString(r.jwtSecret) | ||
|
||
if err != nil { | ||
log.Fatalln("Failed to sign JWT:", err) | ||
} | ||
|
||
http.SetCookie(w, &http.Cookie{ | ||
Name: AUTH_COOKIE_NAME, | ||
HttpOnly: true, | ||
Value: signedTokenString, | ||
MaxAge: 365 * 24 * 60 * 60, // 1 year | ||
SameSite: http.SameSiteLaxMode, | ||
Path: "/", | ||
}) | ||
|
||
nameNum := strings.TrimSuffix(attributes.Email, "@osu.edu") | ||
nameNum = strings.TrimSuffix(nameNum, "@buckeyemail.osu.edu") | ||
|
||
attributes := s.attributesFromContext(r.Context()) | ||
pp.Println(attributes) | ||
student := false | ||
alum := false | ||
employee := false | ||
faculty := false | ||
|
||
fmt.Fprintf(w, "Bye, %s!", attributes.GivenName) | ||
for _, affiliation := range attributes.Affiliations { | ||
if affiliation == "[email protected]" { | ||
student = true | ||
} else if affiliation == "[email protected]" { | ||
alum = true | ||
} else if affiliation == "[email protected]" { | ||
employee = true | ||
} else if affiliation == "[email protected]" { | ||
faculty = true | ||
} | ||
} | ||
|
||
r.db.Exec(` | ||
INSERT OR REPLACE INTO users (idm_id, buck_id, name_num, display_name, student, alum, employee, faculty) | ||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8) | ||
`, attributes.IDMUID, attributes.BuckID, nameNum, attributes.DisplayName, student, alum, employee, faculty) | ||
|
||
redirect := req.URL.Query().Get("redirect") | ||
if redirect != "" { | ||
http.Redirect(w, req, redirect, http.StatusTemporaryRedirect) | ||
return | ||
} | ||
|
||
fmt.Fprintf(w, "Hello, %s!", attributes.GivenName) | ||
} | ||
|
||
func getUserIDFromContext(ctx context.Context) (string, bool) { | ||
userId, ok := ctx.Value(CONTEXT_USER_ID_KEY).(string) | ||
|
||
return userId, ok | ||
} | ||
|
||
func (r *Router) hello(w http.ResponseWriter, req *http.Request) { | ||
userId, hasUserId := getUserIDFromContext(req.Context()) | ||
|
||
if hasUserId { | ||
row := r.db.QueryRow("SELECT display_name FROM users WHERE idm_id = ?", userId) | ||
var displayName string | ||
row.Scan(&displayName) | ||
fmt.Fprintf(w, "Hello, %s!", displayName) | ||
} else { | ||
fmt.Fprintln(w, "Hello, unknown user!") | ||
} | ||
} | ||
|
||
type contextUserIdType int | ||
|
||
const CONTEXT_USER_ID_KEY contextUserIdType = iota | ||
|
||
func (r *Router) InjectJwtMiddleware(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
cookie, err := req.Cookie(AUTH_COOKIE_NAME) | ||
if err != nil { | ||
handler.ServeHTTP(w, req) | ||
return | ||
} | ||
|
||
token, err := jwt.Parse(cookie.Value, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
} | ||
|
||
return r.jwtSecret, nil | ||
}) | ||
if err != nil { | ||
log.Println(err) | ||
http.Redirect(w, req, fmt.Sprintf("/signin?redirect=%v", req.URL.Path), http.StatusTemporaryRedirect) | ||
return | ||
} | ||
|
||
claims, ok := token.Claims.(jwt.MapClaims) | ||
if !ok || !token.Valid { | ||
log.Println("Invalid token", token) | ||
http.Redirect(w, req, fmt.Sprintf("/signin?redirect=%v", req.URL.Path), http.StatusTemporaryRedirect) | ||
return | ||
} | ||
|
||
idm_id := claims["idm_id"].(string) | ||
|
||
req = req.WithContext(context.WithValue(req.Context(), CONTEXT_USER_ID_KEY, idm_id)) | ||
handler.ServeHTTP(w, req) | ||
}) | ||
} | ||
|
||
func (r *Router) EnforceJwtMiddleware(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
_, hasUserId := getUserIDFromContext(req.Context()) | ||
if !hasUserId { | ||
http.Redirect(w, req, fmt.Sprintf("/signin?redirect=%v", req.URL.Path), http.StatusTemporaryRedirect) | ||
return | ||
} | ||
|
||
handler.ServeHTTP(w, req) | ||
}) | ||
} | ||
|
||
//go:embed migrations/* | ||
var migrations embed.FS | ||
|
||
|
@@ -57,7 +187,6 @@ func main() { | |
log.Fatalln("Failed to read", entry.Name(), err) | ||
} | ||
sql := string(data) | ||
fmt.Println(sql) | ||
_, err = db.Exec(sql) | ||
if err != nil { | ||
log.Fatalln("Failed to run", entry.Name(), err) | ||
|
@@ -83,7 +212,25 @@ func main() { | |
authProvider, _ = samlAuthProvider(mux, rootURL, &keyPair) | ||
} | ||
|
||
mux.Handle("/hello", authProvider.requireAuth(http.HandlerFunc(hello(authProvider)))) | ||
jwtSecret := os.Getenv("JWT_SECRET") | ||
if jwtSecret == "" { | ||
if authEnvironment != "" && authEnvironment != "saml" { | ||
log.Fatalln("JWT_SECRET not set") | ||
} | ||
|
||
log.Println("DEFAULTING JWT_SECRET TO `secret` DO NOT RUN IN PRODUCTION") | ||
jwtSecret = "secret" | ||
} | ||
|
||
router := &Router{ | ||
db: db, | ||
authProvider: authProvider, | ||
jwtSecret: []byte(jwtSecret), | ||
} | ||
|
||
mux.Handle("/hello", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(http.HandlerFunc(router.hello)))) | ||
// mux.Handle("/hello", router.InjectJwtMiddleware(http.HandlerFunc(router.hello))) | ||
mux.Handle("/signin", authProvider.requireAuth(http.HandlerFunc(router.signin))) | ||
mux.Handle("/logout", authProvider.requireAuth(http.HandlerFunc(authProvider.globalLogout))) | ||
|
||
if authEnvironment == "saml" { | ||
|
This file contains 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
This file contains 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