docs: token definition

This commit is contained in:
Geno 2021-09-14 21:55:21 +02:00
parent 8fda0be59c
commit 33b1c21c4e
6 changed files with 40 additions and 38 deletions

View File

@ -15,7 +15,7 @@ Request for Register
```xml ```xml
<iq from="push-distributer@example.org/device" to="up.chat.sum7.eu" type="set" id="register-id"> <iq from="push-distributer@example.org/device" to="up.chat.sum7.eu" type="set" id="register-id">
<register xmlns='unifiedpush.org'> <register xmlns='unifiedpush.org'>
<token>token-from-distributer-regisration</token> <token>pubic-token</token>
</register> </register>
</iq> </iq>
``` ```
@ -26,7 +26,7 @@ on success:
```xml ```xml
<iq from="push-distributer@example.org/device" to="up.chat.sum7.eu" type="result" id="register-id"> <iq from="push-distributer@example.org/device" to="up.chat.sum7.eu" type="result" id="register-id">
<register xmlns='unifiedpush.org'> <register xmlns='unifiedpush.org'>
<endpoint>https://an-endpoint-for-application-server.localhost/UP?token=public-token</endpoint> <endpoint>https://an-endpoint-for-application-server.localhost/UP?token=endpoint-token</endpoint>
</register> </register>
</iq> </iq>
``` ```
@ -49,7 +49,7 @@ For the push notification it-self the origin `<message/>` is used with following
```xml ```xml
<message from="up.chat.sum7.eu" to="push-distributer@example.org/device" id="message-id"> <message from="up.chat.sum7.eu" to="push-distributer@example.org/device" id="message-id">
<subject>token-from-distributer-regisration</subject> <subject>public-token</subject>
<body>Here is the Notification content</body> <body>Here is the Notification content</body>
</message> </message>
``` ```

View File

@ -88,16 +88,16 @@ func (s *XMPPService) message(msgHead stanza.Message, t xmlstream.TokenReadEncod
return nil return nil
} }
if msg.Body == "" || msg.Token == "" { if msg.Body == "" || msg.PublicToken == "" {
log.Infof("empty: %v", msgHead) log.Infof("empty: %v", msgHead)
return nil return nil
} }
logger = logger.WithFields(map[string]interface{}{ logger = logger.WithFields(map[string]interface{}{
"publicToken": msg.Token, "publicToken": msg.PublicToken,
"content": msg.Body, "content": msg.Body,
}) })
conn := s.store.GetConnectionbyPublic(msg.Token) conn := s.store.GetConnectionbyPublic(msg.PublicToken)
if conn == nil { if conn == nil {
logger.Warnf("no appID and appToken found for publicToken") logger.Warnf("no appID and appToken found for publicToken")
} }

View File

@ -11,27 +11,27 @@ type JWTSecret string
// JWTToken data field // JWTToken data field
type JWTToken struct { type JWTToken struct {
jwt.StandardClaims jwt.StandardClaims
Token string `json:"token"` PublicToken string `json:"token"`
JID string `json:"jid"` JID string `json:"jid"`
} }
// Generate an jwt token by token and jid // Generate an endpoint token by public token and jid
func (s JWTSecret) Generate(jid jid.JID, token string) (string, error) { func (s JWTSecret) Generate(jid jid.JID, publicToken string) (string, error) {
jwtToken := JWTToken{ jwtToken := JWTToken{
Token: token, PublicToken: publicToken,
JID: jid.String(), JID: jid.String(),
} }
claim := jwt.NewWithClaims(jwt.SigningMethodHS512, jwtToken) claim := jwt.NewWithClaims(jwt.SigningMethodHS512, jwtToken)
t, err := claim.SignedString([]byte(s)) endpointToken, err := claim.SignedString([]byte(s))
if err != nil { if err != nil {
return "", err return "", err
} }
return t, nil return endpointToken, nil
} }
// Read token to token and jid // Read endpoint token to public token and jid
func (s JWTSecret) Read(jwtToken string) (jid.JID, string, error) { func (s JWTSecret) Read(endpointToken string) (jid.JID, string, error) {
token, err := jwt.ParseWithClaims(jwtToken, &JWTToken{}, func(token *jwt.Token) (interface{}, error) { token, err := jwt.ParseWithClaims(endpointToken, &JWTToken{}, func(token *jwt.Token) (interface{}, error) {
return []byte(s), nil return []byte(s), nil
}) })
if err != nil { if err != nil {
@ -45,5 +45,5 @@ func (s JWTSecret) Read(jwtToken string) (jid.JID, string, error) {
if err != nil { if err != nil {
return jid.JID{}, "", err return jid.JID{}, "", err
} }
return addr, claims.Token, nil return addr, claims.PublicToken, nil
} }

View File

@ -1,16 +1,17 @@
package main package main
import ( import (
"github.com/gin-gonic/gin"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/gin-gonic/gin"
"dev.sum7.eu/genofire/golang-lib/web" "dev.sum7.eu/genofire/golang-lib/web"
) )
func Post(r *gin.Engine, ws *web.Service, xmpp *XMPPService, jwtsecret JWTSecret) { func Post(r *gin.Engine, ws *web.Service, xmpp *XMPPService, jwtsecret JWTSecret) {
r.POST("/UP", func(c *gin.Context) { r.POST("/UP", func(c *gin.Context) {
to, token, err := jwtsecret.Read(c.Query("token")) to, publicToken, err := jwtsecret.Read(c.Query("token"))
if err != nil { if err != nil {
c.JSON(http.StatusUnauthorized, web.HTTPError{ c.JSON(http.StatusUnauthorized, web.HTTPError{
Message: "jwt token unauthoried - or not given", Message: "jwt token unauthoried - or not given",
@ -27,7 +28,7 @@ func Post(r *gin.Engine, ws *web.Service, xmpp *XMPPService, jwtsecret JWTSecret
return return
} }
content := string(b) content := string(b)
if err := xmpp.SendMessage(to, token, content); err != nil { if err := xmpp.SendMessage(to, publicToken, content); err != nil {
c.JSON(http.StatusNotFound, web.HTTPError{ c.JSON(http.StatusNotFound, web.HTTPError{
Message: "unable to forward to xmpp", Message: "unable to forward to xmpp",
Error: err.Error(), Error: err.Error(),

View File

@ -86,19 +86,19 @@ func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder,
reply.Register.Error = &messages.ErrorData{Body: "unable decode"} reply.Register.Error = &messages.ErrorData{Body: "unable decode"}
return nil return nil
} }
token := tokenData.Body publicToken := tokenData.Body
if token == "" { if publicToken == "" {
log.Warnf("no token found: %v", token) log.Warnf("no token found: %v", publicToken)
reply.Register.Error = &messages.ErrorData{Body: "no token"} reply.Register.Error = &messages.ErrorData{Body: "no token"}
return nil return nil
} }
jwt, err := s.JWTSecret.Generate(iq.From, token) endpointToken, err := s.JWTSecret.Generate(iq.From, publicToken)
if err != nil { if err != nil {
log.Errorf("unable jwt generation: %v", err) log.Errorf("unable entpointToken generation: %v", err)
reply.Register.Error = &messages.ErrorData{Body: "jwt error on gateway"} reply.Register.Error = &messages.ErrorData{Body: "endpointToken error on gateway"}
return nil return nil
} }
endpoint := s.EndpointURL + "/UP?token=" + jwt endpoint := s.EndpointURL + "/UP?token=" + endpointToken
reply.IQ.Type = stanza.ResultIQ reply.IQ.Type = stanza.ResultIQ
reply.Register.Endpoint = &messages.EndpointData{Body: endpoint} reply.Register.Endpoint = &messages.EndpointData{Body: endpoint}
log.Debugf("generate respone: %v", endpoint) log.Debugf("generate respone: %v", endpoint)
@ -141,10 +141,10 @@ func (s *XMPPService) handleDisco(iq stanza.IQ, t xmlstream.TokenReadEncoder, st
} }
// SendMessage of an UP Notification // SendMessage of an UP Notification
func (s *XMPPService) SendMessage(to jid.JID, token, content string) error { func (s *XMPPService) SendMessage(to jid.JID, publicToken, content string) error {
log.WithFields(map[string]interface{}{ log.WithFields(map[string]interface{}{
"to": to.String(), "to": to.String(),
"token": token, "publicToken": publicToken,
}).Debug("forward message to xmpp") }).Debug("forward message to xmpp")
return s.session.Encode(context.TODO(), messages.Message{ return s.session.Encode(context.TODO(), messages.Message{
Message: stanza.Message{ Message: stanza.Message{
@ -153,7 +153,7 @@ func (s *XMPPService) SendMessage(to jid.JID, token, content string) error {
// Type: stanza.ChatMessage, // Type: stanza.ChatMessage,
Type: stanza.NormalMessage, Type: stanza.NormalMessage,
}, },
Token: token, PublicToken: publicToken,
Body: content, Body: content,
}) })
} }

View File

@ -25,6 +25,7 @@ type RegisterIQ struct {
} `xml:"register"` } `xml:"register"`
} }
// TokenData transport the public token from distributor to gateway
type TokenData struct { type TokenData struct {
XMLName xml.Name `xml:"token"` XMLName xml.Name `xml:"token"`
Body string `xml:",chardata"` Body string `xml:",chardata"`
@ -49,7 +50,7 @@ type UnregisterIQ struct {
// Unregister without stanza // Unregister without stanza
type Unregister struct { type Unregister struct {
XMLName xml.Name `xml:"unifiedpush.org unregister"` XMLName xml.Name `xml:"unifiedpush.org unregister"`
// set // set - public token
Token string `xml:"token,omitempty"` Token string `xml:"token,omitempty"`
// result // result
Success *string `xml:"success,omitempty"` Success *string `xml:"success,omitempty"`
@ -60,12 +61,12 @@ type Unregister struct {
// Message of push notification - with stanza // Message of push notification - with stanza
type Message struct { type Message struct {
stanza.Message stanza.Message
Token string `xml:"subject"` PublicToken string `xml:"subject"`
Body string `xml:"body"` Body string `xml:"body"`
} }
// MessageBody of push notification - without stanza // MessageBody of push notification - without stanza
type MessageBody struct { type MessageBody struct {
Token string `xml:"subject"` PublicToken string `xml:"subject"`
Body string `xml:"body"` Body string `xml:"body"`
} }