goapplesignin

package module
v0.0.0-...-53a63ea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 6 Imported by: 0

README

Go-Apple-Signin

GoLang wrapper around Sign in with Apple REST API. It provides helper functionality around apple's authentication mechanism for Go.

This module lets you authenticate users using Apple account in your go application.

Prerequisites

  1. You should be enrolled in Apple Developer Program.
  2. Please have a look at Apple documentation related to "Sign in with Apple" feature.
  3. You should create App ID and Service ID in your Apple Developer Account.
  4. You should generate private key for your Service ID in your Apple Developer Account.

More detail about configuration can be found in blog post and Apple docs.

Installation

Install the module using go get

go get github.com/BolajiOlajide/go-apple-signin

Usage/Examples

1. Get authorization URL

Start "Sign in with Apple" flow by redirecting user to the authorization URL.

import "github.com/BolajiOlajide/go-apple-signin"

const options = {
    clientID: "com.gotechmakers.auth.client", // identifier of Apple Service ID.
    redirectUri: "http://localhost:3000/auth/apple/callback",
    state: "123", // optional, An unguessable random string. It is primarily used to protect against CSRF attacks.
    scope: "email" // optional, default value is "email".
};

const authorizationUrl = appleSignin.getAuthorizationUrl(options);

Alternatively, you can use Sign In with Apple browser javascript library.

2. Get access token

2.1. Retrieve "code" query param from URL string when user is redirected to your site after successful sign in with Apple. Example: http://localhost:3000/auth/apple/callback?code=somecode&state=123.

2.2. Exchange retrieved "code" to user's access token.

More detail can be found in Apple docs.

const clientSecret = appleSignin.getClientSecret({
    clientID: "com.gotechmakers.auth.client", // identifier of Apple Service ID.
    teamId: "teamId", // Apple Developer Team ID.
    key: "-----BEGIN PRIVATE KEY----- ...", // private key associated with client ID
    keyIdentifier: "XXX" // identifier of the private key.
});

const options = {
    clientID: "com.gotechmakers.auth.client", // identifier of Apple Service ID.
    redirectUri: "http://localhost:3000/auth/apple/callback", // use the same value which you passed to authorisation URL,
    clientSecret: clientSecret
};

appleSignin.getAuthorizationToken(code, options).then(tokenResponse => {
    console.log(tokenResponse);
}).catch(error => {
    console.log(error);
});

Result of getAuthorizationToken command is a JSON object representing Apple's TokenResponse:

{
    access_token: "ACCESS_TOKEN", // A token used to access allowed data.
    token_type: 'Bearer', // It will always be Bearer.
    expires_in: 3600, // The amount of time, in seconds, before the access token expires.
    refresh_token: "REFRESH_TOKEN", // used to regenerate new access tokens. Store this token securely on your server.
    id_token: "ID_TOKEN" // A JSON Web Token that contains the user’s identity information.
}
3. Verify token signature and get unique user's identifier
appleSignin.verifyIdToken(tokenResponse.id_token, clientID).then(result => {
    const userAppleId = result.sub;
}).catch(error => {
    // Token is not verified
    console.log(error);
});
4. Refresh access token after expiration

const clientSecret = appleSignin.getClientSecret({
    clientID: "com.gotechmakers.auth.client", // identifier of Apple Service ID.
    teamId: "teamId", // Apple Developer Team ID.
    privateKeyPath: "/var/www/app/AuthKey_XXX.p8", // path to private key associated with your client ID.
    keyIdentifier: "XXX" // identifier of the private key.
});

const options = {
    clientID: "com.gotechmakers.auth.client", // identifier of Apple Service ID.
    clientSecret: clientSecret
};

appleSignin.refreshAuthorizationToken(refreshToken, options).then(result => {
    const newAccessToken = result.access_token;
}).catch(error => {
    console.log(error);
})

Acknowledgements

Authors

Contributing

Contributions are always welcome! Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Running Tests

To run tests, run the following command

  npm run test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAuthorizationURL

func GetAuthorizationURL(options models.AuthURLOptions) (string, error)

GetAuthorizationURL returns an initiating auth for apple users

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL