| 1 |
package exampleop |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log/slog" |
| 9 |
"net/http" |
| 10 |
"net/url" |
| 11 |
|
| 12 |
"github.com/go-chi/chi/v5" |
| 13 |
"github.com/gorilla/securecookie" |
| 14 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 15 |
) |
| 16 |
|
| 17 |
type deviceAuthenticate interface { |
| 18 |
CheckUsernamePasswordSimple(username, password string) error |
| 19 |
op.DeviceAuthorizationStorage |
| 20 |
|
| 21 |
// GetDeviceAuthorizationByUserCode returns the current state of the device authorization flow, |
| 22 |
// identified by the user code. |
| 23 |
GetDeviceAuthorizationByUserCode(ctx context.Context, userCode string) (*op.DeviceAuthorizationState, error) |
| 24 |
|
| 25 |
// CompleteDeviceAuthorization marks a device authorization entry as Completed, |
| 26 |
// identified by userCode. The Subject is added to the state, so that |
| 27 |
// GetDeviceAuthorizatonState can use it to create a new Access Token. |
| 28 |
CompleteDeviceAuthorization(ctx context.Context, userCode, subject string) error |
| 29 |
|
| 30 |
// DenyDeviceAuthorization marks a device authorization entry as Denied. |
| 31 |
DenyDeviceAuthorization(ctx context.Context, userCode string) error |
| 32 |
} |
| 33 |
|
| 34 |
type deviceLogin struct { |
| 35 |
storage deviceAuthenticate |
| 36 |
cookie *securecookie.SecureCookie |
| 37 |
} |
| 38 |
|
| 39 |
func registerDeviceAuth(storage deviceAuthenticate, router chi.Router) { |
| 40 |
l := &deviceLogin{ |
| 41 |
storage: storage, |
| 42 |
cookie: securecookie.New(securecookie.GenerateRandomKey(32), nil), |
| 43 |
} |
| 44 |
|
| 45 |
router.HandleFunc("/", l.userCodeHandler) |
| 46 |
router.Post("/login", l.loginHandler) |
| 47 |
router.HandleFunc("/confirm", l.confirmHandler) |
| 48 |
} |
| 49 |
|
| 50 |
func renderUserCode(w io.Writer, err error) { |
| 51 |
data := struct { |
| 52 |
Error string |
| 53 |
}{ |
| 54 |
Error: errMsg(err), |
| 55 |
} |
| 56 |
|
| 57 |
if err := templates.ExecuteTemplate(w, "usercode", data); err != nil { |
| 58 |
slog.Error("render user code", "error", err) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func renderDeviceLogin(w http.ResponseWriter, userCode string, err error) { |
| 63 |
data := &struct { |
| 64 |
UserCode string |
| 65 |
Error string |
| 66 |
}{ |
| 67 |
UserCode: userCode, |
| 68 |
Error: errMsg(err), |
| 69 |
} |
| 70 |
if err = templates.ExecuteTemplate(w, "device_login", data); err != nil { |
| 71 |
slog.Error("render device login", "error", err) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func renderConfirmPage(w http.ResponseWriter, username, clientID string, scopes []string) { |
| 76 |
data := &struct { |
| 77 |
Username string |
| 78 |
ClientID string |
| 79 |
Scopes []string |
| 80 |
}{ |
| 81 |
Username: username, |
| 82 |
ClientID: clientID, |
| 83 |
Scopes: scopes, |
| 84 |
} |
| 85 |
if err := templates.ExecuteTemplate(w, "confirm_device", data); err != nil { |
| 86 |
slog.Error("render confirmation page", "error", err) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func (d *deviceLogin) userCodeHandler(w http.ResponseWriter, r *http.Request) { |
| 91 |
err := r.ParseForm() |
| 92 |
if err != nil { |
| 93 |
w.WriteHeader(http.StatusBadRequest) |
| 94 |
renderUserCode(w, err) |
| 95 |
return |
| 96 |
} |
| 97 |
userCode := r.Form.Get("user_code") |
| 98 |
if userCode == "" { |
| 99 |
if prompt, _ := url.QueryUnescape(r.Form.Get("prompt")); prompt != "" { |
| 100 |
err = errors.New(prompt) |
| 101 |
} |
| 102 |
renderUserCode(w, err) |
| 103 |
return |
| 104 |
} |
| 105 |
|
| 106 |
renderDeviceLogin(w, userCode, nil) |
| 107 |
} |
| 108 |
|
| 109 |
func redirectBack(w http.ResponseWriter, r *http.Request, prompt string) { |
| 110 |
values := make(url.Values) |
| 111 |
values.Set("prompt", url.QueryEscape(prompt)) |
| 112 |
|
| 113 |
url := url.URL{ |
| 114 |
Path: "/device", |
| 115 |
RawQuery: values.Encode(), |
| 116 |
} |
| 117 |
http.Redirect(w, r, url.String(), http.StatusSeeOther) |
| 118 |
} |
| 119 |
|
| 120 |
const userCodeCookieName = "user_code" |
| 121 |
|
| 122 |
type userCodeCookie struct { |
| 123 |
UserCode string |
| 124 |
UserName string |
| 125 |
} |
| 126 |
|
| 127 |
func (d *deviceLogin) loginHandler(w http.ResponseWriter, r *http.Request) { |
| 128 |
if err := r.ParseForm(); err != nil { |
| 129 |
redirectBack(w, r, err.Error()) |
| 130 |
return |
| 131 |
} |
| 132 |
|
| 133 |
userCode := r.PostForm.Get("user_code") |
| 134 |
if userCode == "" { |
| 135 |
redirectBack(w, r, "missing user_code in request") |
| 136 |
return |
| 137 |
} |
| 138 |
username := r.PostForm.Get("username") |
| 139 |
if username == "" { |
| 140 |
redirectBack(w, r, "missing username in request") |
| 141 |
return |
| 142 |
} |
| 143 |
password := r.PostForm.Get("password") |
| 144 |
if password == "" { |
| 145 |
redirectBack(w, r, "missing password in request") |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
if err := d.storage.CheckUsernamePasswordSimple(username, password); err != nil { |
| 150 |
redirectBack(w, r, err.Error()) |
| 151 |
return |
| 152 |
} |
| 153 |
state, err := d.storage.GetDeviceAuthorizationByUserCode(r.Context(), userCode) |
| 154 |
if err != nil { |
| 155 |
redirectBack(w, r, err.Error()) |
| 156 |
return |
| 157 |
} |
| 158 |
|
| 159 |
encoded, err := d.cookie.Encode(userCodeCookieName, userCodeCookie{userCode, username}) |
| 160 |
if err != nil { |
| 161 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 162 |
return |
| 163 |
} |
| 164 |
cookie := &http.Cookie{ |
| 165 |
Name: userCodeCookieName, |
| 166 |
Value: encoded, |
| 167 |
Path: "/", |
| 168 |
HttpOnly: true, |
| 169 |
} |
| 170 |
http.SetCookie(w, cookie) |
| 171 |
renderConfirmPage(w, username, state.ClientID, state.Scopes) |
| 172 |
} |
| 173 |
|
| 174 |
func (d *deviceLogin) confirmHandler(w http.ResponseWriter, r *http.Request) { |
| 175 |
cookie, err := r.Cookie(userCodeCookieName) |
| 176 |
if err != nil { |
| 177 |
redirectBack(w, r, err.Error()) |
| 178 |
return |
| 179 |
} |
| 180 |
data := new(userCodeCookie) |
| 181 |
if err = d.cookie.Decode(userCodeCookieName, cookie.Value, &data); err != nil { |
| 182 |
redirectBack(w, r, err.Error()) |
| 183 |
return |
| 184 |
} |
| 185 |
if err = r.ParseForm(); err != nil { |
| 186 |
redirectBack(w, r, err.Error()) |
| 187 |
return |
| 188 |
} |
| 189 |
|
| 190 |
action := r.Form.Get("action") |
| 191 |
switch action { |
| 192 |
case "allowed": |
| 193 |
err = d.storage.CompleteDeviceAuthorization(r.Context(), data.UserCode, data.UserName) |
| 194 |
case "denied": |
| 195 |
err = d.storage.DenyDeviceAuthorization(r.Context(), data.UserCode) |
| 196 |
default: |
| 197 |
err = errors.New("action must be one of \"allow\" or \"deny\"") |
| 198 |
} |
| 199 |
if err != nil { |
| 200 |
redirectBack(w, r, err.Error()) |
| 201 |
return |
| 202 |
} |
| 203 |
|
| 204 |
fmt.Fprintf(w, "Device authorization %s. You can now return to the device", action) |
| 205 |
} |