-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
oauth: add sign-in with apple support (fixes #110) #243
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
646540d
oauth: add sign-in with apple support (fixes #110)
AlexCuse 7b0d5a8
Apply suggestions from code review
AlexCuse 75ebde4
move mockKeyStore to test file
AlexCuse 5645188
fix test + remove credential reference to signing key
AlexCuse 619b2e9
define custom apple claims with validation
AlexCuse 6377550
comment cleanup
AlexCuse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,62 @@ | ||
package oauth | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/keratin/authn-server/lib/oauth/apple" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
// NewAppleProvider returns a AuthN integration for sign-in with Apple OAuth | ||
func NewAppleProvider(credentials *Credentials) (*Provider, error) { | ||
config := &oauth2.Config{ | ||
ClientID: credentials.ID, | ||
// ClientSecret for apple is built using apple.GenerateSecret | ||
// this function is passed to the provider for use as an override | ||
// and built fresh on each call to provider.Config(returnURL). | ||
ClientSecret: "", | ||
Scopes: []string{"email"}, | ||
Endpoint: apple.Endpoint(), | ||
} | ||
|
||
teamID, keyID, expiresIn, constructErr := apple.ExtractCredentialData(credentials.Additional) | ||
if constructErr != nil { | ||
return nil, fmt.Errorf("apple: failed to extract credentials: %w", constructErr) | ||
} | ||
|
||
keyBytes, err := hex.DecodeString(credentials.Secret) | ||
if err != nil { | ||
return nil, fmt.Errorf("apple: failed to decode key from client secret: %w", err) | ||
} | ||
|
||
signingKey, constructErr := apple.ParsePrivateKey(keyBytes, keyID) | ||
if constructErr != nil { | ||
return nil, fmt.Errorf("apple: failed to parse signing key: %w", constructErr) | ||
} | ||
|
||
appleTokenReader := apple.NewTokenReader(config.ClientID) | ||
|
||
getAppleUserInfo := func(t *oauth2.Token) (*UserInfo, error) { | ||
id, email, err := appleTokenReader.GetUserDetailsFromToken(t) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &UserInfo{ | ||
ID: id, | ||
Email: email, | ||
}, nil | ||
} | ||
|
||
return NewProvider(config, getAppleUserInfo, | ||
WithSecretGenerator(func() (string, error) { | ||
return apple.GenerateSecret(*signingKey, keyID, config.ClientID, teamID, expiresIn) | ||
}), | ||
// Apple requires form_post response mode if scopes are requested | ||
WithAuthCodeOptions(oauth2.SetAuthURLParam("response_mode", "form_post")), | ||
// So we need to handle returns via POST instead of GET | ||
WithReturnMethod(http.MethodPost)), nil | ||
} |
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,40 @@ | ||
package apple | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-jose/go-jose/v3/jwt" | ||
) | ||
|
||
type Claims struct { | ||
Email string `json:"email"` | ||
jwt.Claims | ||
} | ||
|
||
// Validate performs apple-specific id_token validation. | ||
// `email` is the only additional claim we currently require. | ||
// See https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple#3383773 | ||
// for more details. | ||
func (c Claims) Validate(clientID string) error { | ||
if clientID == "" { | ||
return fmt.Errorf("cannot validate with empty clientID") | ||
} | ||
|
||
if c.Email == "" { | ||
return fmt.Errorf("missing claim 'email'") | ||
} | ||
|
||
if c.Expiry == nil { | ||
return fmt.Errorf("missing claim 'exp'") | ||
} | ||
|
||
if c.IssuedAt == nil { | ||
return fmt.Errorf("missing claim 'iat'") | ||
} | ||
|
||
// is default 1m leeway OK here? | ||
return c.Claims.Validate(jwt.Expected{ | ||
Issuer: BaseURL, | ||
Audience: jwt.Audience{clientID}, | ||
}) | ||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i checked to confirm that it will validate |
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd think so. if there's some network delay when the user returns to authn, this leeway should handle it. if the token is more than 1m too old, then someone's clock is out of sync and they're going to have bigger problems.