Skip to content

Commit

Permalink
Added check for access token and remove old code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidallendj committed Mar 25, 2024
1 parent 429caf0 commit d4debf8
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions cmd/boot-script-service/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,6 @@ func (client *OAuthClient) CreateOAuthClient(registerUrl string) ([]byte, error)
return b, nil
}

func (client *OAuthClient) AuthorizeOAuthClient(authorizeUrl string) ([]byte, error) {
// encode ID and secret for authorization header basic authentication
// basicAuth := base64.StdEncoding.EncodeToString(
// []byte(fmt.Sprintf("%s:%s",
// url.QueryEscape(client.Id),
// url.QueryEscape(client.Secret),
// )),
// )
body := []byte("grant_type=client_credentials&scope=read&client_id=" + client.Id +
"&client_secret=" + client.Secret +
"&redirect_uri=" + url.QueryEscape("http://hydra:5555/callback") +
"&response_type=token" +
"&state=12345678910",
)
headers := map[string][]string{
"Authorization": {"Bearer " + client.RegistrationAccessToken},
"Content-Type": {"application/x-www-form-urlencoded"},
}

req, err := http.NewRequest(http.MethodPost, authorizeUrl, bytes.NewBuffer(body))
req.Header = headers
if err != nil {
return nil, fmt.Errorf("failed to make request: %v", err)
}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to do request: %v", err)
}
defer res.Body.Close()

return io.ReadAll(res.Body)
}

func (client *OAuthClient) PerformTokenGrant(remoteUrl string) (string, error) {
// hydra endpoint: /oauth/token
body := "grant_type=" + url.QueryEscape("client_credentials") +
Expand Down Expand Up @@ -183,6 +150,11 @@ func (client *OAuthClient) PerformTokenGrant(remoteUrl string) (string, error) {
return "", fmt.Errorf("failed to unmarshal response body: %v", err)
}

accessToken := rjson["access_token"]
if accessToken == nil {
return "", fmt.Errorf("no access token found")
}

return rjson["access_token"].(string), nil
}

Expand Down

0 comments on commit d4debf8

Please sign in to comment.