Skip to content
Snippets Groups Projects
Verified Commit 9f023504 authored by Tóth Miklós Tibor's avatar Tóth Miklós Tibor :shrug:
Browse files

v1

parent 72672b7a
Branches
Tags
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="f8eec622-fc43-45a1-b989-d17928f7d369" name="Default Changelist" comment="" />
<list default="true" id="f8eec622-fc43-45a1-b989-d17928f7d369" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/auth.go" beforeDir="false" afterPath="$PROJECT_DIR$/auth.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......@@ -15,6 +19,9 @@
</option>
</component>
<component name="GOROOT" path="/usr/lib/go" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="KubernetesApiPersistence">
<option name="context" value="KSZK" />
<option name="namespace" value="newbies" />
......@@ -54,5 +61,14 @@
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="760" y="402" key="#Go_Modules/0.1080.1592.899/0.0.1920.1080@0.0.1920.1080" timestamp="1607527088066" />
<state x="560" y="210" key="#Plugins" timestamp="1607533008924">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="560" y="210" key="#Plugins/0.1080.1592.899/0.0.1920.1080@0.0.1920.1080" timestamp="1607533008924" />
<state x="623" y="273" width="672" height="678" key="search.everywhere.popup" timestamp="1607533589166">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="623" y="273" width="672" height="678" key="search.everywhere.popup/0.0.1920.1080/0.1080.1592.899@0.0.1920.1080" timestamp="1607533589166" />
<state x="623" y="273" width="672" height="678" key="search.everywhere.popup/0.1080.1592.899/0.0.1920.1080@0.0.1920.1080" timestamp="1607532994246" />
</component>
</project>
\ No newline at end of file
// AuthSCH bejelentkezés könyvtár Go-hoz
package authsch
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"time"
)
// Egy AuthSCH kliens
type Client struct {
*oauth2.Config
}
// Létrehoz egy klienset. Meg kell adni az AuthSCH-n fejlesztői konzolon kapott azonosítót és kulcsot, illetve a kért scope-okat
// A scope-oktról több infót itt találsz:
// https://git.sch.bme.hu/kszk/authsch/-/wikis/api
func CreateClient(ClientID, ClientSecret string, Scopes []string) Client {
var conf = &oauth2.Config{
ClientID: ClientID,
ClientSecret: ClientSecret,
Scopes: Scopes,
Endpoint: oauth2.Endpoint{
TokenURL: "https://auth.sch.bme.hu/oauth2/token",
AuthURL: "https://auth.sch.bme.hu/site/login",
},
}
return Client{conf}
}
// Egy felhasználó lekérhető adatai.
// A scope-októl függően az InternalID-n kívül a többi mező üres lehet.
// A scope-oktról több infót itt találsz:
// https://git.sch.bme.hu/kszk/authsch/-/wikis/api
type AccDetails struct {
InternalID string `json:"internal_id"`
Surname string `json:"sn"`
GivenName string `json:"givenName"`
Mail string `json:"mail"`
DisplayName string `json:"displayName"`
LinkedAccounts struct {
Bme string `json:"bme"`
SchAcc string `json:"schacc"`
Vir string `json:"vir"`
} `json:"linkedAccounts"`
EduPersonEntitlement []struct {
Id int `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
} `json:"eduPersonEntitlement"`
}
// Ad egy URL-t az AuthSCH-s bejelentkező ablakhoz, ide kell irányítani a usert.
// Az AuthSCH majd visszairányítja a usert a megadott URL-re.
func (c *Client) GetAuthURL() string {
// Redirect user to consent page to ask for permission
// for the scopes specified above.
url := c.Config.AuthCodeURL("state", oauth2.AccessTypeOffline)
return url
}
// Egy http.Handler-t ad vissza, amit be kell kötni arra az URL-re, ahova az AuthSCH átirányít
// 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 {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
code := r.URL.Query().Get("code")
if code == "" {
errorCallback(w, r)
fmt.Println("No code in query params")
return
}
// Use the custom HTTP client when requesting a token.
httpClient := &http.Client{Timeout: 2 * time.Second}
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
tok, err := c.Config.Exchange(ctx, code)
if err != nil {
errorCallback(w, r)
fmt.Println(err)
}
client := c.Config.Client(ctx, tok)
_ = client
b, e := http.Get(fmt.Sprintf("https://auth.sch.bme.hu/api/profile?access_token=%s", tok.AccessToken))
if e != nil {
fmt.Println(e)
errorCallback(w, r)
return
}
bod, _ := ioutil.ReadAll(b.Body)
accDetails := AccDetails{}
e = json.Unmarshal(bod, &accDetails)
if e != nil {
fmt.Println(e)
errorCallback(w, r)
return
}
next(accDetails, w, r)
})
}
......@@ -3,3 +3,5 @@ module git.sch.bme.hu/mikewashere/authsch-go
replace git.sch.bme.hu/mikewashere/authsch-go => ./
go 1.15
require golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 // indirect
go.sum 0 → 100644
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment