Skip to content

Instantly share code, notes, and snippets.

View cp-sumi-k's full-sized avatar

Sumita Kevat cp-sumi-k

View GitHub Profile
/** payload data **/
payload := &NotificationPayload{}
_, err = jwt.ParseWithClaims(tokenStr, payload, func(token *jwt.Token) (interface{}, error) {
return extractPublicKeyFromToken(tokenStr)
})
/** transaction info **/
transactionInfo := &TransactionInfo{}
// Notification signed payload
type NotificationPayload struct {
jwt.StandardClaims
NotificationType string `json:"notificationType"`
Subtype string `json:"subtype"`
NotificationUUID string `json:"notificationUUID"`
NotificationVersion string `json:"notificationVersion"`
Data NotificationData `json:"data"`
}
func extractPublicKeyFromToken(tokenStr string) (*ecdsa.PublicKey, error) {
certStr, err := extractHeaderByIndex(tokenStr, 0) // get certificate from X5c[0] header
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certStr) // parse certificate
if err != nil {
return nil, err
func verifyCert(certByte []byte, intermediateCert []byte) error {
roots := x509.NewCertPool() // new empty set of certificate pool
ok := roots.AppendCertsFromPEM([]byte(APP_STORE_NOTIFICATION_ROOT_CERT)) // parse and append app store certificate to certPool
if !ok {
return errors.New("failed to parse root certificate")
}
interCert, err := x509.ParseCertificate(intermediateCert) // parse and append intermediate X5c certificate
// prepare header structure contains algorithm and token type
type NotificationHeader struct {
Alg string `json:"alg"`
X5c []string `json:"x5c"`
}
// extract header from given JWS formatted token
func extractHeaderByIndex(tokenStr string, index int) ([]byte, error) {
tokenArr := strings.Split(tokenStr, ".") // get header from token
<?php
$text = "\xE0";
echo utf8_decode($text);
# output : ?
?>
<?php
$text = "\xE0";
echo utf8_encode($text);
# output : à
?>
<?php
# encode
echo str_rot13("We celebrate differences!!");
# Output 1 : Jr pryroengr qvssreraprf!!
# decode
<?php
# Example 1 (Associative array)
$users = '[{"id":"1","name":"John"},{"id":"2","name":"Michel"}]';
var_dump(json_decode($users));
/*
output: array(2) { [0]=> object(stdClass)#1 (2) { ["id"]=> string(1) "1" ["name"]=> string(4) "John" }
[1]=> object(stdClass)#2 (2) { ["id"]=> string(1) "2" ["name"]=> string(6) "Michel" }
<?php
# Example 1 (Associative array)
$users = array(array("id"=>"1", "name"=>"John"), array("id"=>"2", "name"=>"Michel"));
echo json_encode($users);
# output: [{"id":"1","name":"John"},{"id":"2","name":"Michel"}]