Skip to content
Snippets Groups Projects
Select Git revision
  • master
  • entra protected
  • kasszasch protected
  • radlaci97-modern-web
  • revert-8b053a7c
  • manifest-background-color
  • 18-ui-pimping protected
  • payout protected
  • v1.8.5 protected
  • v1.8.4 protected
  • v1.8.3 protected
  • v1.8.2 protected
  • v1.8.1 protected
  • v1.8.0 protected
  • v1.7.1 protected
  • v1.7.0 protected
  • v1.6.21 protected
  • v1.6.20 protected
  • v1.6.19 protected
  • v1.6.18 protected
  • v1.6.17 protected
  • v1.6.16 protected
  • v1.6.15 protected
  • v1.16.15 protected
  • v1.6.14 protected
  • v1.6.13 protected
  • v1.6.12 protected
  • v1.6.11 protected
28 results

auth.go

Blame
  • auth.go 2.04 KiB
    package auth
    
    import (
    	"git.sch.bme.hu/disappointment-industries/becskasszasch/db"
    	error2 "git.sch.bme.hu/disappointment-industries/becskasszasch/error"
    	"git.sch.bme.hu/kszk/opensource/authsch-go"
    	"gitlab.com/MikeTTh/env"
    	"net/http"
    	"time"
    )
    
    const cookieName = "session"
    
    func plspanic() string { panic("CLIENTID or CLIENTSECRET env var is missing") }
    
    var auth = authsch.CreateClient(env.GetOrDosomething("CLIENTID", plspanic), env.GetOrDosomething("CLIENTSECRET", plspanic), []string{
    	"basic",
    	"linkedAccounts",
    	"displayName",
    	"eduPersonEntitlement",
    })
    
    var LoginHandler = auth.GetLoginHandler(
    	func(details *authsch.AccDetails, w http.ResponseWriter, r *http.Request) {
    		kszks := false
    		for _, kor := range details.EduPersonEntitlement {
    			if kor.Id == 47 {
    				kszks = true
    			}
    		}
    		if !kszks {
    			error2.ServeErrorPage(w, "Sajnos nincs megfelelő jogosultságod az oldal használatához")
    			return
    		}
    
    		c, e := db.StoreUserAndGiveCookie(details.LinkedAccounts.SchAcc, details.DisplayName)
    		if e != nil {
    			error2.ServeErrorPage(w, "Hiba történt")
    			return
    		}
    
    		http.SetCookie(w, &http.Cookie{
    			Name:    cookieName,
    			Value:   c,
    			Expires: time.Now().AddDate(1, 0, 0),
    			Path:    "/",
    		})
    
    		http.Redirect(w, r, "/", http.StatusFound)
    	},
    	func(w http.ResponseWriter, r *http.Request) {
    		error2.ServeErrorPage(w, "Hiba történt a bejelentkezés közben")
    	},
    )
    
    func LogoutHandler(w http.ResponseWriter, r *http.Request) {
    	cook, e := r.Cookie(cookieName)
    	if e != nil {
    		return
    	}
    
    	db.DeleteCookie(cook.Value) //TODO: hibakezeles
    
    	http.Redirect(w, r, "/", http.StatusFound)
    }
    
    var GetLoginURL = auth.GetAuthURL
    
    func GetCookie(r *http.Request) string {
    	cook, e := r.Cookie(cookieName)