Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview: add helper to create ServiceClients

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
fff56012 f6c2000c

+73
+73
appview/oauth/oauth.go
··· 7 7 "net/url" 8 8 "time" 9 9 10 + indigo_xrpc "github.com/bluesky-social/indigo/xrpc" 10 11 "github.com/gorilla/sessions" 11 12 oauth "tangled.sh/icyphox.sh/atproto-oauth" 12 13 "tangled.sh/icyphox.sh/atproto-oauth/helpers" ··· 205 204 }) 206 205 207 206 return xrpcClient, nil 207 + } 208 + 209 + // use this to create a client to communicate with knots or spindles 210 + // 211 + // this is a higher level abstraction on ServerGetServiceAuth 212 + type ServiceClientOpts struct { 213 + service string 214 + exp int64 215 + lxm string 216 + dev bool 217 + } 218 + 219 + type ServiceClientOpt func(*ServiceClientOpts) 220 + 221 + func WithService(service string) ServiceClientOpt { 222 + return func(s *ServiceClientOpts) { 223 + s.service = service 224 + } 225 + } 226 + func WithExp(exp int64) ServiceClientOpt { 227 + return func(s *ServiceClientOpts) { 228 + s.exp = exp 229 + } 230 + } 231 + 232 + func WithLxm(lxm string) ServiceClientOpt { 233 + return func(s *ServiceClientOpts) { 234 + s.lxm = lxm 235 + } 236 + } 237 + 238 + func WithDev(dev bool) ServiceClientOpt { 239 + return func(s *ServiceClientOpts) { 240 + s.dev = dev 241 + } 242 + } 243 + 244 + func (s *ServiceClientOpts) Audience() string { 245 + return fmt.Sprintf("did:web:%s", s.service) 246 + } 247 + 248 + func (s *ServiceClientOpts) Host() string { 249 + scheme := "https://" 250 + if s.dev { 251 + scheme = "http://" 252 + } 253 + 254 + return scheme + s.service 255 + } 256 + 257 + func (o *OAuth) ServiceClient(r *http.Request, os ...ServiceClientOpt) (*indigo_xrpc.Client, error) { 258 + opts := ServiceClientOpts{} 259 + for _, o := range os { 260 + o(&opts) 261 + } 262 + 263 + authorizedClient, err := o.AuthorizedClient(r) 264 + if err != nil { 265 + return nil, err 266 + } 267 + 268 + resp, err := authorizedClient.ServerGetServiceAuth(r.Context(), opts.Audience(), opts.exp, opts.lxm) 269 + if err != nil { 270 + return nil, err 271 + } 272 + 273 + return &indigo_xrpc.Client{ 274 + Auth: &indigo_xrpc.AuthInfo{ 275 + AccessJwt: resp.Token, 276 + }, 277 + Host: opts.Host(), 278 + }, nil 208 279 } 209 280 210 281 type ClientMetadata struct {