-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working transformer parking sfhera, without pushing to bdp
- Loading branch information
Showing
8 changed files
with
604 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
transformers/parking-offstreet-sfhera/src/bdplib/auth.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// SPDX-FileCopyrightText: NOI Techpark <[email protected]> | ||
|
||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
package bdplib | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Token struct { | ||
AccessToken string `json:"access_token"` | ||
ExpiresIn int64 `json:"expires_in"` | ||
NotBeforePolicy int64 `json:"not-before-policy"` | ||
RefreshExpiresIn int64 `json:"refresh_expires_in"` | ||
TokenType string `json:"token_type"` | ||
RefreshToken string `json:"refresh_token"` | ||
Scope string | ||
} | ||
|
||
type Auth struct { | ||
TokenUrl string | ||
ClientId string | ||
ClientSecret string | ||
token Token | ||
tokenExpiry int64 | ||
} | ||
|
||
func AuthFromEnv() *Auth { | ||
a := Auth{} | ||
a.TokenUrl = os.Getenv("ODH_TOKEN_URL") | ||
a.ClientId = os.Getenv("ODH_CLIENT_ID") | ||
a.ClientSecret = os.Getenv("ODH_CLIENT_SECRET") | ||
return &a | ||
} | ||
|
||
func (a *Auth) getToken() string { | ||
ts := time.Now().Unix() | ||
|
||
if len(a.token.AccessToken) == 0 || ts > a.tokenExpiry { | ||
// if no token is available or refreshToken is expired, get new token | ||
a.newToken() | ||
} | ||
|
||
return a.token.AccessToken | ||
} | ||
|
||
func (a *Auth) newToken() { | ||
slog.Info("Getting new token...") | ||
params := url.Values{} | ||
params.Add("client_id", a.ClientId) | ||
params.Add("client_secret", a.ClientSecret) | ||
params.Add("grant_type", "client_credentials") | ||
|
||
a.authRequest(params) | ||
|
||
slog.Info("Getting new token done.") | ||
} | ||
|
||
func (a *Auth) authRequest(params url.Values) { | ||
body := strings.NewReader(params.Encode()) | ||
|
||
req, err := http.NewRequest("POST", a.TokenUrl, body) | ||
if err != nil { | ||
slog.Error("error", err) | ||
return | ||
} | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
slog.Error("error", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
slog.Info("Auth response code is: " + strconv.Itoa(resp.StatusCode)) | ||
if resp.StatusCode == http.StatusOK { | ||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
slog.Error("error", err) | ||
return | ||
} | ||
|
||
err = json.Unmarshal(bodyBytes, &a.token) | ||
if err != nil { | ||
slog.Error("error", err) | ||
return | ||
} | ||
} | ||
|
||
// calculate token expiry timestamp with 600 seconds margin | ||
a.tokenExpiry = time.Now().Unix() + a.token.ExpiresIn - 600 | ||
|
||
slog.Debug("auth token expires in " + strconv.FormatInt(a.tokenExpiry, 10)) | ||
} |
Oops, something went wrong.