Skip to content

Commit

Permalink
Add RedirectURI to GetAuthorizationURLOptions (#45)
Browse files Browse the repository at this point in the history
* Add `RedirectURI` to `GetAuthorizationURLOptions`

* Revert `sso.Configure` README changes
  • Loading branch information
Rohan Jadvani authored Jun 22, 2020
1 parent 5caa906 commit d59b0a2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions pkg/sso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {

http.Handle("/login", sso.Login(sso.GetAuthorizationURLOptions{
Domain: "mydomain.com",
RedirectURI: "https://mydomain.com/callback",
}))

http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
Expand Down
15 changes: 13 additions & 2 deletions pkg/sso/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Client struct {
// The callback URL where your app redirects the user-agent after an
// authorization code is granted (eg. https://foo.com/callback).
//
// REQUIRED.
// Deprecated: Use `GetAuthorizationURLOptions.RedirectURI` instead.
RedirectURI string

// The endpoint to WorkOS API.
Expand Down Expand Up @@ -88,6 +88,12 @@ type GetAuthorizationURLOptions struct {
// Provider is currently only used when the connection type is GoogleOAuth.
Provider ConnectionType

// The callback URL where your app redirects the user-agent after an
// authorization code is granted (eg. https://foo.com/callback).
//
// REQUIRED.
RedirectURI string

// A unique identifier used to manage state across authorization
// transactions (eg. 1234zyx).
//
Expand All @@ -100,9 +106,14 @@ type GetAuthorizationURLOptions struct {
func (c *Client) GetAuthorizationURL(opts GetAuthorizationURLOptions) (*url.URL, error) {
c.once.Do(c.init)

redirectURI := opts.RedirectURI
if redirectURI == "" {
redirectURI = c.RedirectURI
}

query := make(url.Values, 5)
query.Set("client_id", c.ProjectID)
query.Set("redirect_uri", c.RedirectURI)
query.Set("redirect_uri", redirectURI)
query.Set("response_type", "code")

if opts.Domain == "" && opts.Provider == "" {
Expand Down
33 changes: 18 additions & 15 deletions pkg/sso/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,36 @@ func TestClientAuthorizeURL(t *testing.T) {
{
scenario: "generate url",
options: GetAuthorizationURLOptions{
Domain: "lyft.com",
Domain: "lyft.com",
RedirectURI: "https://example.com/sso/workos/callback",
},
expected: "https://api.workos.com/sso/authorize?client_id=proj_123&domain=lyft.com&redirect_uri=https%3A%2F%2Fexample.com%2Fsso%2Fworkos%2Fcallback&response_type=code",
},
{
scenario: "generate url with state",
options: GetAuthorizationURLOptions{
Domain: "lyft.com",
State: "custom state",
Domain: "lyft.com",
RedirectURI: "https://example.com/sso/workos/callback",
State: "custom state",
},
expected: "https://api.workos.com/sso/authorize?client_id=proj_123&domain=lyft.com&redirect_uri=https%3A%2F%2Fexample.com%2Fsso%2Fworkos%2Fcallback&response_type=code&state=custom+state",
},
{
scenario: "generate url with provider",
options: GetAuthorizationURLOptions{
Provider: "GoogleOAuth",
State: "custom state",
Provider: "GoogleOAuth",
RedirectURI: "https://example.com/sso/workos/callback",
State: "custom state",
},
expected: "https://api.workos.com/sso/authorize?client_id=proj_123&provider=GoogleOAuth&redirect_uri=https%3A%2F%2Fexample.com%2Fsso%2Fworkos%2Fcallback&response_type=code&state=custom+state",
},
{
scenario: "generate url with provider and domain",
options: GetAuthorizationURLOptions{
Domain: "lyft.com",
Provider: "GoogleOAuth",
State: "custom state",
Domain: "lyft.com",
Provider: "GoogleOAuth",
RedirectURI: "https://example.com/sso/workos/callback",
State: "custom state",
},
expected: "https://api.workos.com/sso/authorize?client_id=proj_123&domain=lyft.com&provider=GoogleOAuth&redirect_uri=https%3A%2F%2Fexample.com%2Fsso%2Fworkos%2Fcallback&response_type=code&state=custom+state",
},
Expand All @@ -55,9 +59,8 @@ func TestClientAuthorizeURL(t *testing.T) {
for _, test := range tests {
t.Run(test.scenario, func(t *testing.T) {
client := Client{
APIKey: "test",
ProjectID: "proj_123",
RedirectURI: "https://example.com/sso/workos/callback",
APIKey: "test",
ProjectID: "proj_123",
}

u, err := client.GetAuthorizationURL(test.options)
Expand All @@ -69,13 +72,13 @@ func TestClientAuthorizeURL(t *testing.T) {

func TestClientAuthorizeURLWithNoDomainAndProvider(t *testing.T) {
client := Client{
APIKey: "test",
ProjectID: "proj_123",
RedirectURI: "https://example.com/sso/workos/callback",
APIKey: "test",
ProjectID: "proj_123",
}

u, err := client.GetAuthorizationURL(GetAuthorizationURLOptions{
State: "state",
RedirectURI: "https://example.com/sso/workos/callback",
State: "state",
})

require.Error(t, err)
Expand Down
23 changes: 12 additions & 11 deletions pkg/sso/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
// )
//
// http.Handle("/login", sso.Login(sso.GetAuthorizationURLOptions{
// Domain: "mydomain.com",
// Domain: "mydomain.com",
// RedirectURI: "https://mydomain.com/callback",
// }))
//
// http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
// profile, err := sso.GetProfile(context.Background(), sso.GetProfileOptions{
// Code: r.URL.Query().Get("code"),
// })
// if err != nil {
// // Handle the error ...
// return
// }
// http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
// profile, err := sso.GetProfile(context.Background(), sso.GetProfileOptions{
// Code: r.URL.Query().Get("code"),
// })
// if err != nil {
// // Handle the error ...
// return
// }
//
// // Handle the profile ...
// fmt.Println(profile)
// // Handle the profile ...
// fmt.Println(profile)
// })
//
// if err := http.ListenAndServe("your_server_addr", nil); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/sso/sso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func TestLogin(t *testing.T) {
wg.Add(1)

mux.Handle("/login", Login(GetAuthorizationURLOptions{
Domain: "lyft.com",
Domain: "lyft.com",
RedirectURI: redirectURI,
}))

mux.HandleFunc("/sso/authorize", func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit d59b0a2

Please sign in to comment.