コード例 #1
0
ファイル: jwt.go プロジェクト: alternaDev/georenting-server
// GenerateJWTToken generates a JWT token for a given UserID and signs it with
// the given private key. The token will be valid for 3 days.
func GenerateJWTToken(user models.User) (string, error) {
	if user.PrivateKey == "" {
		privateKey, err := GenerateNewPrivateKey()

		if err != nil {
			return "", err
		}

		user.PrivateKey = PrivateKeyToString(privateKey)
		user.Save()
		if err != nil {
			return "", err
		}
	}

	privateKey, err := StringToPrivateKey(user.PrivateKey)

	if err != nil {
		return "", err
	}

	token := jwt.New(jwt.SigningMethodRS256)

	token.Claims["user"] = user.ID
	token.Header["user"] = user.ID
	token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
	token.Header["exp"] = token.Claims["exp"]

	tokenString, err := token.SignedString(privateKey)

	return tokenString, err
}
コード例 #2
0
// CreateDeviceGroup creates a new Device group on Google Cloud Messaging
func CreateDeviceGroup(firstID string, user *models.User) error {
	response, err := sendGCMGroupRequest(gcmGroupRequest{
		Operation:           "create",
		NotificationKeyName: os.Getenv("BASE_URL") + "-GeoRenting-" + strconv.Itoa(user.ID) + "-" + user.Name,
		RegistrationIDs:     []string{firstID},
	})

	if err != nil {
		return err
	}

	if response.Error != "" {
		return errors.New(response.Error)
	}

	user.GCMNotificationID = response.NotificationKey
	err = user.Save()
	if err != nil {
		return err
	}

	return nil
}