aboutsummaryrefslogtreecommitdiff
path: root/cmd/didweb/atproto-recommended-credentials.go
blob: 2b870c00d42338bfe4bed8d9796dda30e9b4faa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/bluesky-social/indigo/xrpc"
	"github.com/whyrusleeping/go-did"
	"within.website/x/web"
)

type RecommendedCredentials struct {
	AlsoKnownAs         []string `json:"alsoKnownAs"`
	VerificationMethods struct {
		Atproto did.DID `json:"atproto"`
	} `json:"verificationMethods"`
	RotationKeys []string `json:"rotationKeys"`
	Services     struct {
		AtprotoPds struct {
			Type     string `json:"type"`
			Endpoint string `json:"endpoint"`
		} `json:"atproto_pds"`
	} `json:"services"`
}

func IdentityGetRecommendedDidCredentials(ctx context.Context, cli *xrpc.Client) (*RecommendedCredentials, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, cli.Host+"/xrpc/com.atproto.identity.getRecommendedDidCredentials", nil)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Authorization", "Bearer "+cli.Auth.AccessJwt)
	req.Header.Add("Accept", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, web.NewError(http.StatusOK, resp)
	}

	var result RecommendedCredentials
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}

	return &result, nil
}