aboutsummaryrefslogtreecommitdiff
path: root/internal/utils/ssh_helpers.go
blob: 9ffcf0a525b6c0acad41627b76ecc88b23017126 (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
package utils

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

func GetPubKeyFromService(username, service string) ([]byte, error) {
	// TODO: Read custom services from config

	services := map[string][2]string{
		"github"  : [2]string{"https://github.com/", ".keys"}, 
		"gitlab"  : [2]string{"https://gitlab.com/", ".keys"},
		"codeberg": [2]string{"https://codeberg.org", ".keys"},
	}

	url := strings.TrimSuffix(services[service][0], "/") + "/" + username + services[service][1]

	resp, err := http.Get(url)
	if err != nil {
		return nil, fmt.Errorf("failed to fetch key from %s: %w", service, err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("%s returned status %d", service, resp.StatusCode)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read response from %s: %w", service, err)
	}

	return body, nil
}