-
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.
Allow supplying client id/secret for auth
Allow a user to specify a PCBe Client ID and PCBe Client Secret as an alternative to a token. Either (1) a token or (2) credentials (id/secret) must be provided. The code will use the creds to generate a token when a token is not provided directly. Currently the environment variables being used are `PCBE_CLIENT_ID` and `PCBE_CLIENT_SECRET`. These are defined as constants and may be changed later (eg when integrating with the "umbrella" hpegl provider) if desired.
- Loading branch information
1 parent
ec116ba
commit cce24ee
Showing
4 changed files
with
231 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
) | ||
|
||
func TestAuthOk(t *testing.T) { | ||
mockURL := "http://example.com/api" | ||
mockBody := `grant_type=client_credentials&client_id=id123&client_secret=secret123` | ||
expectedToken := "token123" | ||
|
||
defer gock.Off() | ||
|
||
gock.New(mockURL). | ||
Post("/api"). | ||
MatchHeader("Content-Type", "application/x-www-form-urlencoded"). | ||
JSON(mockBody). | ||
Reply(200). | ||
JSON(map[string]string{"access_token": expectedToken}) | ||
|
||
creds := Credentials{ | ||
URL: mockURL, | ||
ID: "id123", | ||
Secret: "secret123", | ||
} | ||
|
||
client := &http.Client{Transport: &http.Transport{}} | ||
gock.InterceptClient(client) | ||
|
||
token, err := GetToken(context.Background(), creds, WithHTTPClient(client)) | ||
if err != nil { | ||
t.Fatalf("GetToken failed: %v", err) | ||
} | ||
|
||
if token != expectedToken { | ||
t.Fatalf("GetToken returned unexpected value: %v", token) | ||
} | ||
} | ||
|
||
func TestAuthBad(t *testing.T) { | ||
mockURL := "http://example.com/api" | ||
mockBody := `grant_type=client_credentials&client_id=id123&client_secret=secret123` | ||
|
||
defer gock.Off() | ||
|
||
gock.New(mockURL). | ||
Post("/api"). | ||
MatchHeader("Content-Type", "application/x-www-form-urlencoded"). | ||
JSON(mockBody). | ||
Reply(401). | ||
BodyString("invalid") | ||
|
||
creds := Credentials{ | ||
URL: mockURL, | ||
ID: "id123", | ||
Secret: "secret123", | ||
} | ||
|
||
client := &http.Client{Transport: &http.Transport{}} | ||
gock.InterceptClient(client) | ||
|
||
_, err := GetToken(context.Background(), creds, WithHTTPClient(client)) | ||
if err == nil { | ||
t.Fatalf("GetToken should have failed") | ||
} | ||
|
||
if err.Error() != "failed to get token (http code: 401 Unauthorized)" { | ||
t.Fatalf("GetToken returned unexpected error: %v", err) | ||
} | ||
} |
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