MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / Save

Method Save

internal/store/objectstore.go:159–235  ·  view source on GitHub ↗

Save persists authentication metadata to disk and uploads it to the object storage backend.

(ctx context.Context, auth *cliproxyauth.Auth)

Source from the content-addressed store, hash-verified

157
158// Save persists authentication metadata to disk and uploads it to the object storage backend.
159func (s *ObjectTokenStore) Save(ctx context.Context, auth *cliproxyauth.Auth) (string, error) {
160 if auth == nil {
161 return "", fmt.Errorf("object store: auth is nil")
162 }
163
164 path, err := s.resolveAuthPath(auth)
165 if err != nil {
166 return "", err
167 }
168 if path == "" {
169 return "", fmt.Errorf("object store: missing file path attribute for %s", auth.ID)
170 }
171
172 if auth.Disabled {
173 if _, statErr := os.Stat(path); errors.Is(statErr, fs.ErrNotExist) {
174 return "", nil
175 }
176 }
177
178 s.mu.Lock()
179 defer s.mu.Unlock()
180
181 if err = os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
182 return "", fmt.Errorf("object store: create auth directory: %w", err)
183 }
184
185 switch {
186 case auth.Storage != nil:
187 if auth.Metadata == nil {
188 auth.Metadata = make(map[string]any)
189 }
190 auth.Metadata["disabled"] = auth.Disabled
191 if setter, ok := auth.Storage.(interface{ SetMetadata(map[string]any) }); ok {
192 setter.SetMetadata(auth.Metadata)
193 }
194 if err = auth.Storage.SaveTokenToFile(path); err != nil {
195 return "", err
196 }
197 case auth.Metadata != nil:
198 auth.Metadata["disabled"] = auth.Disabled
199 raw, errMarshal := json.Marshal(auth.Metadata)
200 if errMarshal != nil {
201 return "", fmt.Errorf("object store: marshal metadata: %w", errMarshal)
202 }
203 if existing, errRead := os.ReadFile(path); errRead == nil {
204 if jsonEqual(existing, raw) {
205 return path, nil
206 }
207 } else if errRead != nil && !errors.Is(errRead, fs.ErrNotExist) {
208 return "", fmt.Errorf("object store: read existing metadata: %w", errRead)
209 }
210 tmp := path + ".tmp"
211 if errWrite := os.WriteFile(tmp, raw, 0o600); errWrite != nil {
212 return "", fmt.Errorf("object store: write temp auth file: %w", errWrite)
213 }
214 if errRename := os.Rename(tmp, path); errRename != nil {
215 return "", fmt.Errorf("object store: rename auth file: %w", errRename)
216 }

Callers

nothing calls this directly

Calls 5

resolveAuthPathMethod · 0.95
uploadAuthMethod · 0.95
jsonEqualFunction · 0.70
SetMetadataMethod · 0.65
SaveTokenToFileMethod · 0.65

Tested by

no test coverage detected