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
<iq from="push-distributer@example.org/device" to="up.chat.sum7.eu" type="set" id="register-id">
<register xmlns='unifiedpush.org'>
<token>token-from-distributer-regisration</token>
<token>pubic-token</token>
</register>
</iq>
```
@ -26,7 +26,7 @@ on success:
```xml
<iq from="push-distributer@example.org/device" to="up.chat.sum7.eu" type="result" id="register-id">
<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>
</iq>
```
@ -49,7 +49,7 @@ For the push notification it-self the origin `<message/>` is used with following
```xml
<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>
</message>
```

View File

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

View File

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

View File

@ -1,16 +1,17 @@
package main
import (
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"dev.sum7.eu/genofire/golang-lib/web"
)
func Post(r *gin.Engine, ws *web.Service, xmpp *XMPPService, jwtsecret JWTSecret) {
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 {
c.JSON(http.StatusUnauthorized, web.HTTPError{
Message: "jwt token unauthoried - or not given",
@ -27,7 +28,7 @@ func Post(r *gin.Engine, ws *web.Service, xmpp *XMPPService, jwtsecret JWTSecret
return
}
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{
Message: "unable to forward to xmpp",
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"}
return nil
}
token := tokenData.Body
if token == "" {
log.Warnf("no token found: %v", token)
publicToken := tokenData.Body
if publicToken == "" {
log.Warnf("no token found: %v", publicToken)
reply.Register.Error = &messages.ErrorData{Body: "no token"}
return nil
}
jwt, err := s.JWTSecret.Generate(iq.From, token)
endpointToken, err := s.JWTSecret.Generate(iq.From, publicToken)
if err != nil {
log.Errorf("unable jwt generation: %v", err)
reply.Register.Error = &messages.ErrorData{Body: "jwt error on gateway"}
log.Errorf("unable entpointToken generation: %v", err)
reply.Register.Error = &messages.ErrorData{Body: "endpointToken error on gateway"}
return nil
}
endpoint := s.EndpointURL + "/UP?token=" + jwt
endpoint := s.EndpointURL + "/UP?token=" + endpointToken
reply.IQ.Type = stanza.ResultIQ
reply.Register.Endpoint = &messages.EndpointData{Body: 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
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{}{
"to": to.String(),
"token": token,
"publicToken": publicToken,
}).Debug("forward message to xmpp")
return s.session.Encode(context.TODO(), messages.Message{
Message: stanza.Message{
@ -153,7 +153,7 @@ func (s *XMPPService) SendMessage(to jid.JID, token, content string) error {
// Type: stanza.ChatMessage,
Type: stanza.NormalMessage,
},
Token: token,
PublicToken: publicToken,
Body: content,
})
}

View File

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