Skip to content

Commit

Permalink
Expose api for fetching profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron@nanu-c.org committed Aug 28, 2022
1 parent 70cf775 commit 348b61e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
4 changes: 0 additions & 4 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ func SetUsername(name string) {
config.ConfigFile.Name = name
saveConfig(config.ConfigFile)
}

func RefreshOwnProfile() {

}
51 changes: 41 additions & 10 deletions profiles/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"

zkgroup "github.com/nanu-c/zkgroup"
Expand All @@ -15,13 +16,16 @@ import (
"github.com/signal-golang/textsecure/contacts"
"github.com/signal-golang/textsecure/crypto"
"github.com/signal-golang/textsecure/transport"
"github.com/signal-golang/textsecure/unidentifiedAccess"
log "github.com/sirupsen/logrus"
)

const (
PROFILE_PATH = "/v1/profile/%s"
PROFILE_CREDENTIAL_PATH = "/v1/profile/%s/%s/%s"
NAME_PADDED_LENGTH = 53
PROFILE_USERNAME_PATH = "/v1/profile/username/%s"

NAME_PADDED_LENGTH = 53
)

// Profile ...
Expand Down Expand Up @@ -108,7 +112,7 @@ func encryptName(key, input []byte, paddedLength int) ([]byte, error) {

func decryptName(key, nonceAndCiphertext []byte) ([]byte, error) {
if len(nonceAndCiphertext) < 12+16+1 {
return nil, errors.New("nonceAndCipher too short")
return nil, errors.New("decrypt Name nonceAndCipher too short " + string(len(nonceAndCiphertext)))
}
nonce := nonceAndCiphertext[:12]
ciphertext := nonceAndCiphertext[12:]
Expand Down Expand Up @@ -148,26 +152,49 @@ type Profile struct {
}

func GetProfile(UUID string, profileKey []byte) (*Profile, error) {
resp, err := transport.Transport.Get(fmt.Sprintf(PROFILE_PATH, UUID))
unidentifiedAccess, err := unidentifiedAccess.GetAccessForSync(config.ConfigFile.ProfileKey, config.ConfigFile.Certificate)
if err != nil {
return nil, err
}
resp, err := transport.Transport.GetWithUnidentifiedAccessKey(fmt.Sprintf(PROFILE_PATH, UUID), unidentifiedAccess.UnidentifiedAccessKey)
if err != nil {
return nil, err
}
profile := &Profile{}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, profile)

dec := json.NewDecoder(resp.Body)
err = dec.Decode(&profile)
if err != nil {
log.Debugln("[textsecure] GetProfile", err)
log.Debugln("[textsecure] GetProfile decode error", err)
return nil, err
} else {
err = decryptProfile(profileKey, profile)
if err != nil {
log.Errorln("[textsecure] decrypt profile error", err)
// return nil, err
}
resp, err = transport.Transport.GetWithUnidentifiedAccessKey(fmt.Sprintf(PROFILE_PATH, UUID), []byte(profile.UnidentifiedAccess))
if err != nil {
return profile, err
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, profile)
if err != nil {
log.Debugln("[textsecure] GetProfile decode error", err)
return nil, err
}
}
return profile, nil

}
func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {
log.Infoln("[textsecure] GetProfileAndCredential")
log.Infoln("[textsecure] GetProfileAndCredential for " + UUID)
uuid, err := uuidUtil.FromString(UUID)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
Expand Down Expand Up @@ -198,10 +225,14 @@ func GetProfileAndCredential(UUID string, profileKey []byte) (*Profile, error) {
return nil, err
}
profile := &Profile{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&profile)
bytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential", err)
return nil, err
}
// fmt.Printf("%s\n", string(bytes))
err = json.Unmarshal(bytes, profile)
if err != nil {
log.Debugln("[textsecure] GetProfileAndCredential json unmarshall", err)
return nil, err
} else {
err = decryptProfile(profileKey, profile)
Expand Down
25 changes: 25 additions & 0 deletions transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -73,6 +74,7 @@ type Transporter interface {
PutBinary(url string, body []byte) (*response, error)
PutJSONWithAuth(url string, body []byte, auth string) (*response, error)
PutJSONWithUnidentifiedSender(url string, body []byte, unidentifiedAccessKey []byte) (*response, error)
GetWithUnidentifiedAccessKey(url string, unidentifiedAccessKey []byte) (*response, error)
}

type httpTransporter struct {
Expand Down Expand Up @@ -243,6 +245,29 @@ func (ht *httpTransporter) PutWithAuth(url string, body []byte, ct string, auth

return r, err
}
func (ht *httpTransporter) GetWithUnidentifiedAccessKey(url string, unidentifedAccessKey []byte) (*response, error) {
req, err := http.NewRequest("GET", ht.baseURL+url, nil)
if err != nil {
return nil, err
}
if ht.userAgent != "" {
req.Header.Set("X-Signal-Agent", ht.userAgent)
}
req.Header.Set("Unidentified-Access-Key", base64.StdEncoding.EncodeToString(unidentifedAccessKey))
resp, err := ht.client.Do(req)
if err != nil {
return nil, err
}
r := &response{}
if resp != nil {
r.Status = resp.StatusCode
r.Body = resp.Body
}

log.Debugf("[textsecure] GET with unidentified access key %s %d\n", url, r.Status)

return r, err
}
func (ht *httpTransporter) PutWithUnidentifiedSender(url string, body []byte, ct string, unidentifiedAccessKey []byte) (*response, error) {
br := bytes.NewReader(body)
req, err := http.NewRequest("PUT", ht.baseURL+url, br)
Expand Down

0 comments on commit 348b61e

Please sign in to comment.