File: winlink_account.go

package info (click to toggle)
pat 0.19.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,228 kB
  • sloc: javascript: 3,864; sh: 147; makefile: 11
file content (94 lines) | stat: -rw-r--r-- 2,660 bytes parent folder | download
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package api

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

	"github.com/la5nta/pat/internal/cmsapi"
)

func (h Handler) winlinkAccountRegistrationHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		callsign := h.Options().MyCall
		if v := r.URL.Query().Get("callsign"); v != "" {
			callsign = v
		}
		if callsign == "" {
			http.Error(w, "Empty callsign", http.StatusBadRequest)
			return
		}
		exists, err := cmsapi.AccountExists(r.Context(), callsign)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		json.NewEncoder(w).Encode(struct {
			Callsign string `json:"callsign"`
			Exists   bool   `json:"exists"`
		}{callsign, exists})
	case http.MethodPost:
		type body struct {
			Callsign      string `json:"callsign"`
			Password      string `json:"password"`
			RecoveryEmail string `json:"recovery_email"` // optional
		}
		var v body
		if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		switch {
		case v.Callsign == "":
			http.Error(w, "Empty callsign", http.StatusBadRequest)
			return
		case len(v.Password) < 6 || len(v.Password) > 12:
			http.Error(w, "Password must be 6-12 characters", http.StatusBadRequest)
			return
		}
		if err := cmsapi.AccountAdd(r.Context(), v.Callsign, v.Password, v.RecoveryEmail); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		json.NewEncoder(w).Encode(v)
	}
}

func (h Handler) winlinkPasswordRecoveryEmailHandler(w http.ResponseWriter, r *http.Request) {
	type body struct {
		RecoveryEmail string `json:"recovery_email"`
	}

	var (
		ctx      = r.Context()
		callsign = h.Options().MyCall
		password = h.Config().SecureLoginPassword
	)
	if callsign == "" || password == "" {
		http.Error(w, "Missing callsign or password in config", http.StatusBadRequest)
		return
	}

	switch r.Method {
	case http.MethodGet:
		email, err := cmsapi.PasswordRecoveryEmailGet(ctx, callsign, password)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(body{RecoveryEmail: email})
	case http.MethodPut:
		var v body
		if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		if err := cmsapi.PasswordRecoveryEmailSet(ctx, callsign, password, v.RecoveryEmail); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(v)
	}
}