-
Notifications
You must be signed in to change notification settings - Fork 3
/
oauth.go
66 lines (48 loc) · 1.66 KB
/
oauth.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package thirdpartyauthentication
import (
"errors"
"github.com/Glintt/gAPI/api/utils"
// "github.com/Glintt/gAPI/api/config"
// "github.com/Glintt/gAPI/api/config"
"github.com/Glintt/gAPI/api/http"
"github.com/valyala/fasthttp"
)
type OAuthServer struct {
Host string
Port string
AuthorizeEndpoint string
UserTokenInformation UserTokenInformation
}
func GetAuthorizeEndpointUrl(oas OAuthServer) string {
return oas.Host + ":" + oas.Port + oas.AuthorizeEndpoint
}
/*
func LoadFromConfig() OAuthServer {
authenticationJSON, err := utils.LoadJsonFile(config.CONFIGS_LOCATION + config.AUTHENTICATION_CONFIG_FILE)
if err != nil {
return OAuthServer{}
}
var oas OAuthServer
json.Unmarshal(authenticationJSON, &oas)
oas.AuthorizeEndpoint = oas.Host + ":" + oas.Port + oas.AuthorizeEndpoint
return oas
} */
func (oauthServer *OAuthServer) Authorize(request fasthttp.Request) (string, string, error) {
token := request.Header.Peek("Authorization")
if token == nil {
return "", "", errors.New("Not Authorized.")
}
// TODO: send on header
var headers map[string]string
headers = make(map[string]string)
headers["Authorization"] = string(token)
utils.LogMessage("ThirdPartyOAuth (AuthorizationEndpoint) : "+oauthServer.AuthorizeEndpoint, utils.DebugLogType)
response := http.MakeRequest("GET", oauthServer.AuthorizeEndpoint, "", headers)
if response.StatusCode() != 200 {
return "", "", errors.New("Not Authorized.")
}
if oauthServer.UserTokenInformation.Active {
return oauthServer.UserTokenInformation.Name, string(response.Header.Peek(oauthServer.UserTokenInformation.Name)), nil
}
return "token", "valid", nil
}