github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/registry/remote/credentials/example_test.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package credentials_test 15 16 import ( 17 "context" 18 "fmt" 19 "net/http" 20 21 "oras.land/oras-go/v2/registry/remote" 22 "oras.land/oras-go/v2/registry/remote/auth" 23 credentials "oras.land/oras-go/v2/registry/remote/credentials" 24 ) 25 26 func ExampleNewNativeStore() { 27 ns := credentials.NewNativeStore("pass") 28 29 ctx := context.Background() 30 // save credentials into the store 31 err := ns.Put(ctx, "localhost:5000", auth.Credential{ 32 Username: "username-example", 33 Password: "password-example", 34 }) 35 if err != nil { 36 panic(err) 37 } 38 39 // get credentials from the store 40 cred, err := ns.Get(ctx, "localhost:5000") 41 if err != nil { 42 panic(err) 43 } 44 fmt.Println(cred) 45 46 // delete the credentials from the store 47 err = ns.Delete(ctx, "localhost:5000") 48 if err != nil { 49 panic(err) 50 } 51 } 52 53 func ExampleNewFileStore() { 54 fs, err := credentials.NewFileStore("example/path/config.json") 55 if err != nil { 56 panic(err) 57 } 58 59 ctx := context.Background() 60 // save credentials into the store 61 err = fs.Put(ctx, "localhost:5000", auth.Credential{ 62 Username: "username-example", 63 Password: "password-example", 64 }) 65 if err != nil { 66 panic(err) 67 } 68 69 // get credentials from the store 70 cred, err := fs.Get(ctx, "localhost:5000") 71 if err != nil { 72 panic(err) 73 } 74 fmt.Println(cred) 75 76 // delete the credentials from the store 77 err = fs.Delete(ctx, "localhost:5000") 78 if err != nil { 79 panic(err) 80 } 81 } 82 83 func ExampleNewStore() { 84 // NewStore returns a Store based on the given configuration file. It will 85 // automatically determine which Store (file store or native store) to use. 86 // If the native store is not available, you can save your credentials in 87 // the configuration file by specifying AllowPlaintextPut: true, but keep 88 // in mind that this is an unsafe workaround. 89 // See the documentation for details. 90 store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{ 91 AllowPlaintextPut: true, 92 }) 93 if err != nil { 94 panic(err) 95 } 96 97 ctx := context.Background() 98 // save credentials into the store 99 err = store.Put(ctx, "localhost:5000", auth.Credential{ 100 Username: "username-example", 101 Password: "password-example", 102 }) 103 if err != nil { 104 panic(err) 105 } 106 107 // get credentials from the store 108 cred, err := store.Get(ctx, "localhost:5000") 109 if err != nil { 110 panic(err) 111 } 112 fmt.Println(cred) 113 114 // delete the credentials from the store 115 err = store.Delete(ctx, "localhost:5000") 116 if err != nil { 117 panic(err) 118 } 119 } 120 121 func ExampleNewStoreFromDocker() { 122 ds, err := credentials.NewStoreFromDocker(credentials.StoreOptions{ 123 AllowPlaintextPut: true, 124 }) 125 if err != nil { 126 panic(err) 127 } 128 129 ctx := context.Background() 130 // save credentials into the store 131 err = ds.Put(ctx, "localhost:5000", auth.Credential{ 132 Username: "username-example", 133 Password: "password-example", 134 }) 135 if err != nil { 136 panic(err) 137 } 138 139 // get credentials from the store 140 cred, err := ds.Get(ctx, "localhost:5000") 141 if err != nil { 142 panic(err) 143 } 144 fmt.Println(cred) 145 146 // delete the credentials from the store 147 err = ds.Delete(ctx, "localhost:5000") 148 if err != nil { 149 panic(err) 150 } 151 } 152 153 func ExampleNewStoreWithFallbacks_configAsPrimaryStoreDockerAsFallback() { 154 primaryStore, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{ 155 AllowPlaintextPut: true, 156 }) 157 if err != nil { 158 panic(err) 159 } 160 fallbackStore, err := credentials.NewStoreFromDocker(credentials.StoreOptions{}) 161 sf := credentials.NewStoreWithFallbacks(primaryStore, fallbackStore) 162 163 ctx := context.Background() 164 // save credentials into the store 165 err = sf.Put(ctx, "localhost:5000", auth.Credential{ 166 Username: "username-example", 167 Password: "password-example", 168 }) 169 if err != nil { 170 panic(err) 171 } 172 173 // get credentials from the store 174 cred, err := sf.Get(ctx, "localhost:5000") 175 if err != nil { 176 panic(err) 177 } 178 fmt.Println(cred) 179 180 // delete the credentials from the store 181 err = sf.Delete(ctx, "localhost:5000") 182 if err != nil { 183 panic(err) 184 } 185 } 186 187 func ExampleLogin() { 188 store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{ 189 AllowPlaintextPut: true, 190 }) 191 if err != nil { 192 panic(err) 193 } 194 registry, err := remote.NewRegistry("localhost:5000") 195 if err != nil { 196 panic(err) 197 } 198 cred := auth.Credential{ 199 Username: "username-example", 200 Password: "password-example", 201 } 202 err = credentials.Login(context.Background(), store, registry, cred) 203 if err != nil { 204 panic(err) 205 } 206 fmt.Println("Login succeeded") 207 } 208 209 func ExampleLogout() { 210 store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{}) 211 if err != nil { 212 panic(err) 213 } 214 err = credentials.Logout(context.Background(), store, "localhost:5000") 215 if err != nil { 216 panic(err) 217 } 218 fmt.Println("Logout succeeded") 219 } 220 221 func ExampleCredential() { 222 store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{}) 223 if err != nil { 224 panic(err) 225 } 226 227 client := auth.DefaultClient 228 client.Credential = credentials.Credential(store) 229 230 request, err := http.NewRequest(http.MethodGet, "localhost:5000", nil) 231 if err != nil { 232 panic(err) 233 } 234 235 _, err = client.Do(request) 236 if err != nil { 237 panic(err) 238 } 239 }