Skip to content
Snippets Groups Projects
Select Git revision
  • c7d8b897f8e98f00a11d3765b3b58410aef3ffec
  • master default protected
  • dev
  • dgraph
4 results

console.sh

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)
    	if e != nil {
    		return ""
    	}
    
    	return cook.Value
    }
    
    func GetFullSession(r *http.Request) *db.Session {
    	cook := GetCookie(r)
    	u, e := db.GetFullSessionFromCookie(cook)
    	if e != nil {
    		return nil
    	}
    	return u
    }
    
    func GetSession(r *http.Request) *db.Session {
    	cook := GetCookie(r)
    	s, e := db.GetSession(cook)
    	if e != nil {
    		return nil
    	}
    	return s
    }