8
8
"github.com/google/go-querystring/query"
9
9
"github.com/workos/workos-go/v2/internal/workos"
10
10
"github.com/workos/workos-go/v2/pkg/common"
11
+ "github.com/workos/workos-go/v2/pkg/mfa"
11
12
"github.com/workos/workos-go/v2/pkg/workos_errors"
12
13
"net/http"
13
14
"time"
@@ -160,7 +161,8 @@ type AuthenticateWithMagicAuthOpts struct {
160
161
}
161
162
162
163
type AuthenticationResponse struct {
163
- User User `json:"user"`
164
+ Factor mfa.Factor `json:"authentication_factor"`
165
+ Challenge mfa.Challenge `json:"authentication_challenge"`
164
166
}
165
167
166
168
type SendVerificationEmailOpts struct {
@@ -210,6 +212,11 @@ type RemoveUserFromOrganizationOpts struct {
210
212
Organization string `json:"organization_id"`
211
213
}
212
214
215
+ type EnrollAuthFactorOpts struct {
216
+ User string
217
+ Type mfa.FactorType `json:"type"`
218
+ }
219
+
213
220
func NewClient (apiKey string ) * Client {
214
221
return & Client {
215
222
APIKey : apiKey ,
@@ -547,7 +554,7 @@ func (c *Client) RemoveUserFromOrganization(ctx context.Context, opts RemoveUser
547
554
}
548
555
549
556
// AuthenticateWithPassword authenticates a user with Email and Password
550
- func (c * Client ) AuthenticateWithPassword (ctx context.Context , opts AuthenticateWithPasswordOpts ) (AuthenticationResponse , error ) {
557
+ func (c * Client ) AuthenticateWithPassword (ctx context.Context , opts AuthenticateWithPasswordOpts ) (UserResponse , error ) {
551
558
payload := struct {
552
559
AuthenticateWithPasswordOpts
553
560
ClientSecret string `json:"client_secret"`
@@ -560,7 +567,7 @@ func (c *Client) AuthenticateWithPassword(ctx context.Context, opts Authenticate
560
567
561
568
jsonData , err := json .Marshal (payload )
562
569
if err != nil {
563
- return AuthenticationResponse {}, err
570
+ return UserResponse {}, err
564
571
}
565
572
566
573
req , err := http .NewRequest (
@@ -570,7 +577,7 @@ func (c *Client) AuthenticateWithPassword(ctx context.Context, opts Authenticate
570
577
)
571
578
572
579
if err != nil {
573
- return AuthenticationResponse {}, err
580
+ return UserResponse {}, err
574
581
}
575
582
576
583
// Add headers and context to the request
@@ -581,24 +588,24 @@ func (c *Client) AuthenticateWithPassword(ctx context.Context, opts Authenticate
581
588
// Execute the request
582
589
res , err := c .HTTPClient .Do (req )
583
590
if err != nil {
584
- return AuthenticationResponse {}, err
591
+ return UserResponse {}, err
585
592
}
586
593
defer res .Body .Close ()
587
594
588
595
if err = workos_errors .TryGetHTTPError (res ); err != nil {
589
- return AuthenticationResponse {}, err
596
+ return UserResponse {}, err
590
597
}
591
598
592
599
// Parse the JSON response
593
- var body AuthenticationResponse
600
+ var body UserResponse
594
601
dec := json .NewDecoder (res .Body )
595
602
err = dec .Decode (& body )
596
603
597
604
return body , err
598
605
}
599
606
600
607
// AuthenticateWithCode authenticates an OAuth user or a managed SSO user that is logging in through SSO
601
- func (c * Client ) AuthenticateWithCode (ctx context.Context , opts AuthenticateWithCodeOpts ) (AuthenticationResponse , error ) {
608
+ func (c * Client ) AuthenticateWithCode (ctx context.Context , opts AuthenticateWithCodeOpts ) (UserResponse , error ) {
602
609
payload := struct {
603
610
AuthenticateWithCodeOpts
604
611
ClientSecret string `json:"client_secret"`
@@ -611,7 +618,7 @@ func (c *Client) AuthenticateWithCode(ctx context.Context, opts AuthenticateWith
611
618
612
619
jsonData , err := json .Marshal (payload )
613
620
if err != nil {
614
- return AuthenticationResponse {}, err
621
+ return UserResponse {}, err
615
622
}
616
623
617
624
req , err := http .NewRequest (
@@ -621,7 +628,7 @@ func (c *Client) AuthenticateWithCode(ctx context.Context, opts AuthenticateWith
621
628
)
622
629
623
630
if err != nil {
624
- return AuthenticationResponse {}, err
631
+ return UserResponse {}, err
625
632
}
626
633
627
634
// Add headers and context to the request
@@ -632,16 +639,16 @@ func (c *Client) AuthenticateWithCode(ctx context.Context, opts AuthenticateWith
632
639
// Execute the request
633
640
res , err := c .HTTPClient .Do (req )
634
641
if err != nil {
635
- return AuthenticationResponse {}, err
642
+ return UserResponse {}, err
636
643
}
637
644
defer res .Body .Close ()
638
645
639
646
if err = workos_errors .TryGetHTTPError (res ); err != nil {
640
- return AuthenticationResponse {}, err
647
+ return UserResponse {}, err
641
648
}
642
649
643
650
// Parse the JSON response
644
- var body AuthenticationResponse
651
+ var body UserResponse
645
652
dec := json .NewDecoder (res .Body )
646
653
err = dec .Decode (& body )
647
654
@@ -650,7 +657,7 @@ func (c *Client) AuthenticateWithCode(ctx context.Context, opts AuthenticateWith
650
657
651
658
// AuthenticateWithMagicAuth authenticates a user by verifying a one-time code sent to the user's email address by
652
659
// the Magic Auth Send Code endpoint.
653
- func (c * Client ) AuthenticateWithMagicAuth (ctx context.Context , opts AuthenticateWithMagicAuthOpts ) (AuthenticationResponse , error ) {
660
+ func (c * Client ) AuthenticateWithMagicAuth (ctx context.Context , opts AuthenticateWithMagicAuthOpts ) (UserResponse , error ) {
654
661
payload := struct {
655
662
AuthenticateWithMagicAuthOpts
656
663
ClientSecret string `json:"client_secret"`
@@ -663,7 +670,7 @@ func (c *Client) AuthenticateWithMagicAuth(ctx context.Context, opts Authenticat
663
670
664
671
jsonData , err := json .Marshal (payload )
665
672
if err != nil {
666
- return AuthenticationResponse {}, err
673
+ return UserResponse {}, err
667
674
}
668
675
669
676
req , err := http .NewRequest (
@@ -673,7 +680,7 @@ func (c *Client) AuthenticateWithMagicAuth(ctx context.Context, opts Authenticat
673
680
)
674
681
675
682
if err != nil {
676
- return AuthenticationResponse {}, err
683
+ return UserResponse {}, err
677
684
}
678
685
679
686
// Add headers and context to the request
@@ -684,16 +691,16 @@ func (c *Client) AuthenticateWithMagicAuth(ctx context.Context, opts Authenticat
684
691
// Execute the request
685
692
res , err := c .HTTPClient .Do (req )
686
693
if err != nil {
687
- return AuthenticationResponse {}, err
694
+ return UserResponse {}, err
688
695
}
689
696
defer res .Body .Close ()
690
697
691
698
if err = workos_errors .TryGetHTTPError (res ); err != nil {
692
- return AuthenticationResponse {}, err
699
+ return UserResponse {}, err
693
700
}
694
701
695
702
// Parse the JSON response
696
- var body AuthenticationResponse
703
+ var body UserResponse
697
704
dec := json .NewDecoder (res .Body )
698
705
err = dec .Decode (& body )
699
706
@@ -906,3 +913,45 @@ func (c *Client) SendMagicAuthCode(ctx context.Context, opts SendMagicAuthCodeOp
906
913
907
914
return body , err
908
915
}
916
+
917
+ func (c * Client ) EnrollAuthFactor (ctx context.Context , opts EnrollAuthFactorOpts ) (AuthenticationResponse , error ) {
918
+ endpoint := fmt .Sprintf (
919
+ "%s/users/%s/auth/factors" ,
920
+ c .Endpoint ,
921
+ opts .User ,
922
+ )
923
+
924
+ data , err := c .JSONEncode (opts )
925
+ if err != nil {
926
+ return AuthenticationResponse {}, err
927
+ }
928
+
929
+ req , err := http .NewRequest (
930
+ http .MethodPost ,
931
+ endpoint ,
932
+ bytes .NewBuffer (data ),
933
+ )
934
+ if err != nil {
935
+ return AuthenticationResponse {}, err
936
+ }
937
+ req = req .WithContext (ctx )
938
+ req .Header .Set ("User-Agent" , "workos-go/" + workos .Version )
939
+ req .Header .Set ("Authorization" , "Bearer " + c .APIKey )
940
+ req .Header .Set ("Content-Type" , "application/json" )
941
+
942
+ res , err := c .HTTPClient .Do (req )
943
+ if err != nil {
944
+ return AuthenticationResponse {}, err
945
+ }
946
+ defer res .Body .Close ()
947
+
948
+ if err = workos_errors .TryGetHTTPError (res ); err != nil {
949
+ return AuthenticationResponse {}, err
950
+ }
951
+
952
+ var body AuthenticationResponse
953
+ dec := json .NewDecoder (res .Body )
954
+ err = dec .Decode (& body )
955
+
956
+ return body , err
957
+ }
0 commit comments