This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
52 lines (48 loc) · 1.78 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gokreta
import (
"encoding/json"
)
// AuthDetails struct stores information that is
// required to access the API.
type AuthDetails struct {
InstituteCode string
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
// GetAuthDetailsByCredetinals will return you an
// AuthDetails for further use.
func GetAuthDetailsByCredetinals(instituteCode string,
userName string,
password string,
) (AuthDetails, error) {
var authDetails AuthDetails
body := "institute_code=" + instituteCode + "&userName=" + userName + "&password=" + password + "&grant_type=password&client_id=" + CLIENT_ID
headers := map[string]string{
"Content-type": "application/x-www-form-urlencoded; charset=utf-8",
}
respBody, err := MakeRequest("POST", "https://"+instituteCode+".e-kreta.hu/idp/api/v1/Token", headers, body)
if err != nil {
return authDetails, err
}
err = json.Unmarshal(respBody, &authDetails)
authDetails.InstituteCode = instituteCode
return authDetails, err
}
// RefreshAuthDetails will request you a new AuthDetails
// if your current one becomes obsolete.
func RefreshAuthDetails(instituteCode string, refreshToken string) (AuthDetails, error) {
var authDetails AuthDetails
body := "institute_code=" + instituteCode + "&refresh_token=" + refreshToken + "&grant_type=refresh_token&client_id=" + CLIENT_ID
headers := map[string]string{
"Content-type": "application/x-www-form-urlencoded; charset=utf-8",
}
respBody, err := MakeRequest("POST", "https://"+instituteCode+".e-kreta.hu/idp/api/v1/Token", headers, body)
if err != nil {
return authDetails, err
}
err = json.Unmarshal(respBody, &authDetails)
authDetails.InstituteCode = instituteCode
return authDetails, err
}