diff --git a/auth.go b/auth.go index 7341215d76172e8ce4b9a929b493bc8d945ca1b2..49ec4a188378e483efc3301c77b1d2db8900648a 100644 --- a/auth.go +++ b/auth.go @@ -46,13 +46,25 @@ type AccDetails struct { LinkedAccounts struct { Bme string `json:"bme"` SchAcc string `json:"schacc"` - Vir string `json:"vir"` + Vir int `json:"vir"` + VirUid string `json:"virUid"` } `json:"linkedAccounts"` + LinkedAccountsLastSync struct { + Bme int `json:"bme"` + SchAcc int `json:"schacc"` + Vir int `json:"vir"` + } `json:"lastSync"` EduPersonEntitlement []struct { - Id int `json:"id"` - Name string `json:"name"` - Status string `json:"status"` + Id int `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + Title []string `json:"title"` + Start string `json:"start"` + End string `json:"end"` } `json:"eduPersonEntitlement"` + Mobile string `json:"mobile"` + ADMembership []string `json:"admembership"` + BMEUnitScope []string `json:"bmeunitscope"` } // Ad egy URL-t az AuthSCH-s bejelentkező ablakhoz, ide kell irányítani a usert. @@ -69,7 +81,7 @@ func (c *Client) GetAuthURL() string { // A next paraméter megkapja a user adatait a details változóban, illetve egy http.ResponseWriter-t és *http.Request-et is, // így az a függvény célszerűen eltárolja a user adatait, beállít egy sütit, majd átirányítja. // Az errorCallback paraméterben megadott függvény pedig hibás belépés esetén fut le, ott célszerű egy hibaoldalt megjeleníteni. -func (c *Client) GetLoginHandler(next func(details AccDetails, w http.ResponseWriter, r *http.Request), errorCallback func(w http.ResponseWriter, r *http.Request)) http.Handler { +func (c *Client) GetLoginHandler(next func(details *AccDetails, w http.ResponseWriter, r *http.Request), errorCallback func(w http.ResponseWriter, r *http.Request)) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := context.Background() @@ -99,8 +111,8 @@ func (c *Client) GetLoginHandler(next func(details AccDetails, w http.ResponseWr return } bod, _ := ioutil.ReadAll(b.Body) - accDetails := AccDetails{} - e = json.Unmarshal(bod, &accDetails) + accDetails := &AccDetails{} + e = json.Unmarshal(bod, accDetails) if e != nil { fmt.Println(e) errorCallback(w, r) diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000000000000000000000000000000000000..44ee4be4d4b64e905b9f6863ffdb2fbf9f13e136 --- /dev/null +++ b/example_test.go @@ -0,0 +1,52 @@ +package authsch + +import ( + "encoding/json" + "log" + "net/http" +) + +const authRedirect = "/login" + +var auth = CreateClient( + "26736548200992581545", + "HnweMcB0YNOadjcx9WvS0fsLPiX2pS6w6rRIKTHigV0A6bx4yjMxE60TdWKJoG7kxNs9g1gDMgDH92Bw", + []string{ + "basic", + "displayName", + "sn", + "givenName", + "mail", + "linkedAccounts", + "eduPersonEntitlement", + "mobile", + "niifEduPersonAttendedCourse", // nem jó + "entrants", // nem tudom mi + "admembership", + "bmeunitscope", + }) + +var authHandler = auth.GetLoginHandler(func(details *AccDetails, w http.ResponseWriter, r *http.Request) { + b, e := json.MarshalIndent(details, "", " ") + if e != nil { + panic(e) + } + _, _ = w.Write(b) +}, + func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("Couldn't log you in")) + }) + +var mux = http.NewServeMux() + +func init() { + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, auth.GetAuthURL(), http.StatusFound) + }) + mux.Handle("/login/", authHandler) +} + +func ExampleAccDetails() { + log.Fatal(http.ListenAndServe(":8080", mux)) + // Output: +} diff --git a/go.mod b/go.mod index 89706686d5bb8ac6926cfb2cc5a4922ab8f994e9..7eaaa6011108d14101d5609c5835dd5d652c7c37 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,7 @@ replace git.sch.bme.hu/mikewashere/authsch-go => ./ go 1.15 -require golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 // indirect +require ( + golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 + google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 +) diff --git a/go.sum b/go.sum index f0b104ae570c5a4bb25ca915a81c00a678d2f95a..34f71f97ce9f6421588de6c58ed8ce87e5de1688 100644 --- a/go.sum +++ b/go.sum @@ -320,6 +320,7 @@ google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEY google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 h1:PDIOdWxZ8eRizhKa1AAvY53xsvLB1cWorMjslvY3VA8= google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=