-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
173 additions
and
33 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
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,87 @@ | ||
package social | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Timothylock/go-signin-with-apple/apple" | ||
) | ||
|
||
type Apple struct { | ||
isAndroid bool | ||
teamID string | ||
clientID string | ||
androidClientID string | ||
keyID string | ||
privateKey string | ||
} | ||
|
||
func NewAppleSocial( | ||
teamID string, | ||
clientID string, | ||
androidClientID string, | ||
keyID string, | ||
privateKey string, | ||
) IUserData { | ||
return &Apple{ | ||
teamID: teamID, | ||
clientID: clientID, | ||
androidClientID: androidClientID, | ||
keyID: keyID, | ||
privateKey: privateKey, | ||
} | ||
} | ||
|
||
func (a *Apple) GetUserData(ctx context.Context, token string) (*UserData, error) { | ||
if a.isAndroid && a.androidClientID == "" { | ||
return nil, fmt.Errorf("you must set androidClientID") | ||
} | ||
if !a.isAndroid && a.clientID == "" { | ||
Check failure on line 39 in service/component/social/apple.go GitHub Actions / Quality & Security checks
|
||
return nil, fmt.Errorf("you must set clientID") | ||
} | ||
|
||
clientID := a.clientID | ||
if a.isAndroid { | ||
clientID = a.androidClientID | ||
} | ||
|
||
secret, err := apple.GenerateClientSecret(a.privateKey, a.teamID, clientID, a.keyID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := apple.New() | ||
|
||
req := apple.AppValidationTokenRequest{ | ||
ClientID: clientID, | ||
ClientSecret: secret, | ||
Code: token, | ||
} | ||
|
||
var resp apple.ValidationResponse | ||
|
||
err = client.VerifyAppToken(ctx, req, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.Error != "" { | ||
return nil, fmt.Errorf(resp.Error) | ||
} | ||
|
||
claim, err := apple.GetClaims(resp.IDToken) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
emailClaim, ok := (*claim)["email"] | ||
if !ok { | ||
return nil, fmt.Errorf("apple returned claims with 'email' missling") | ||
} | ||
|
||
return &UserData{Email: emailClaim.(string)}, nil | ||
} | ||
|
||
func (a *Apple) SetIsAndroid(isAndroid bool) { | ||
a.isAndroid = isAndroid | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/coretrix/hitrix/service/component/social" | ||
Check failure on line 6 in service/component/social/mocks/social.go GitHub Actions / Quality & Security checks
|
||
"github.com/stretchr/testify/mock" | ||
Check failure on line 7 in service/component/social/mocks/social.go GitHub Actions / Quality & Security checks
|
||
) | ||
|
||
type Social struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *Social) GetUserData(_ context.Context, token string) (*social.UserData, error) { | ||
return m.Called(token).Get(0).(*social.UserData), m.Called(token).Error(1) | ||
} | ||
|
||
func (m *Social) SetIsAndroid(_ bool) {} |
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
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