context.go (7385B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package gtscontext 19 20 import ( 21 "context" 22 "net/url" 23 24 "github.com/go-fed/httpsig" 25 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 26 ) 27 28 // package private context key type. 29 type ctxkey uint 30 31 const ( 32 // context keys. 33 _ ctxkey = iota 34 barebonesKey 35 fastFailKey 36 outgoingPubKeyIDKey 37 requestIDKey 38 receivingAccountKey 39 requestingAccountKey 40 otherIRIsKey 41 httpSigVerifierKey 42 httpSigKey 43 httpSigPubKeyIDKey 44 ) 45 46 // RequestID returns the request ID associated with context. This value will usually 47 // be set by the request ID middleware handler, either pulling an existing supplied 48 // value from request headers, or generating a unique new entry. This is useful for 49 // tying together log entries associated with an original incoming request. 50 func RequestID(ctx context.Context) string { 51 id, _ := ctx.Value(requestIDKey).(string) 52 return id 53 } 54 55 // SetRequestID stores the given request ID value and returns the wrapped 56 // context. See RequestID() for further information on the request ID value. 57 func SetRequestID(ctx context.Context, id string) context.Context { 58 return context.WithValue(ctx, requestIDKey, id) 59 } 60 61 // OutgoingPublicKeyID returns the public key ID (URI) associated with context. This 62 // value is useful for logging situations in which a given public key URI is 63 // relevant, e.g. for outgoing requests being signed by the given key. 64 func OutgoingPublicKeyID(ctx context.Context) string { 65 id, _ := ctx.Value(outgoingPubKeyIDKey).(string) 66 return id 67 } 68 69 // SetOutgoingPublicKeyID stores the given public key ID value and returns the wrapped 70 // context. See PublicKeyID() for further information on the public key ID value. 71 func SetOutgoingPublicKeyID(ctx context.Context, id string) context.Context { 72 return context.WithValue(ctx, outgoingPubKeyIDKey, id) 73 } 74 75 // ReceivingAccount returns the local account who owns the resource being 76 // interacted with (inbox, uri, etc) in the current ActivityPub request chain. 77 func ReceivingAccount(ctx context.Context) *gtsmodel.Account { 78 acct, _ := ctx.Value(receivingAccountKey).(*gtsmodel.Account) 79 return acct 80 } 81 82 // SetReceivingAccount stores the given receiving account value and returns the wrapped 83 // context. See ReceivingAccount() for further information on the receiving account value. 84 func SetReceivingAccount(ctx context.Context, acct *gtsmodel.Account) context.Context { 85 return context.WithValue(ctx, receivingAccountKey, acct) 86 } 87 88 // RequestingAccount returns the remote account interacting with a local 89 // resource (inbox, uri, etc) in the current ActivityPub request chain. 90 func RequestingAccount(ctx context.Context) *gtsmodel.Account { 91 acct, _ := ctx.Value(requestingAccountKey).(*gtsmodel.Account) 92 return acct 93 } 94 95 // SetRequestingAccount stores the given requesting account value and returns the wrapped 96 // context. See RequestingAccount() for further information on the requesting account value. 97 func SetRequestingAccount(ctx context.Context, acct *gtsmodel.Account) context.Context { 98 return context.WithValue(ctx, requestingAccountKey, acct) 99 } 100 101 // OtherIRIs returns other IRIs which are involved in the current ActivityPub request 102 // chain. This usually means: other accounts who are mentioned, CC'd, TO'd, or boosted 103 // by the current inbox POST request. 104 func OtherIRIs(ctx context.Context) []*url.URL { 105 iris, _ := ctx.Value(otherIRIsKey).([]*url.URL) 106 return iris 107 } 108 109 // SetOtherIRIs stores the given IRIs slice and returns the wrapped context. 110 // See OtherIRIs() for further information on the IRIs slice value. 111 func SetOtherIRIs(ctx context.Context, iris []*url.URL) context.Context { 112 return context.WithValue(ctx, otherIRIsKey, iris) 113 } 114 115 // HTTPSignatureVerifier returns an http signature verifier for the current ActivityPub 116 // request chain. This verifier can be called to authenticate the current request. 117 func HTTPSignatureVerifier(ctx context.Context) httpsig.Verifier { 118 verifier, _ := ctx.Value(httpSigVerifierKey).(httpsig.Verifier) 119 return verifier 120 } 121 122 // SetHTTPSignatureVerifier stores the given http signature verifier and returns the 123 // wrapped context. See HTTPSignatureVerifier() for further information on the verifier value. 124 func SetHTTPSignatureVerifier(ctx context.Context, verifier httpsig.Verifier) context.Context { 125 return context.WithValue(ctx, httpSigVerifierKey, verifier) 126 } 127 128 // HTTPSignature returns the http signature string 129 // value for the current ActivityPub request chain. 130 func HTTPSignature(ctx context.Context) string { 131 signature, _ := ctx.Value(httpSigKey).(string) 132 return signature 133 } 134 135 // SetHTTPSignature stores the given http signature string and returns the wrapped 136 // context. See HTTPSignature() for further information on the verifier value. 137 func SetHTTPSignature(ctx context.Context, signature string) context.Context { 138 return context.WithValue(ctx, httpSigKey, signature) 139 } 140 141 // HTTPSignaturePubKeyID returns the public key id of the http signature 142 // for the current ActivityPub request chain. 143 func HTTPSignaturePubKeyID(ctx context.Context) *url.URL { 144 pubKeyID, _ := ctx.Value(httpSigPubKeyIDKey).(*url.URL) 145 return pubKeyID 146 } 147 148 // SetHTTPSignaturePubKeyID stores the given http signature public key id and returns 149 // the wrapped context. See HTTPSignaturePubKeyID() for further information on the value. 150 func SetHTTPSignaturePubKeyID(ctx context.Context, pubKeyID *url.URL) context.Context { 151 return context.WithValue(ctx, httpSigPubKeyIDKey, pubKeyID) 152 } 153 154 // IsFastFail returns whether the "fastfail" context key has been set. This 155 // can be used to indicate to an http client, for example, that the result 156 // of an outgoing request is time sensitive and so not to bother with retries. 157 func IsFastfail(ctx context.Context) bool { 158 _, ok := ctx.Value(fastFailKey).(struct{}) 159 return ok 160 } 161 162 // SetFastFail sets the "fastfail" context flag and returns this wrapped context. 163 // See IsFastFail() for further information on the "fastfail" context flag. 164 func SetFastFail(ctx context.Context) context.Context { 165 return context.WithValue(ctx, fastFailKey, struct{}{}) 166 } 167 168 // Barebones returns whether the "barebones" context key has been set. This 169 // can be used to indicate to the database, for example, that only a barebones 170 // model need be returned, Allowing it to skip populating sub models. 171 func Barebones(ctx context.Context) bool { 172 _, ok := ctx.Value(barebonesKey).(struct{}) 173 return ok 174 } 175 176 // SetBarebones sets the "barebones" context flag and returns this wrapped context. 177 // See Barebones() for further information on the "barebones" context flag. 178 func SetBarebones(ctx context.Context) context.Context { 179 return context.WithValue(ctx, barebonesKey, struct{}{}) 180 }