Dynamic auth
Resolving a strategy dynamically in runtime
Currently, Gosip requires choosing a specific strategy or strategies to import and use within the application. However, some applications or utility tools might need more. There is no official universal resolver (yet it's planned), but in an application, some dynamic can be added on demand.
The sample shows a simple way of importing potentially demanded strategies and selecting one in runtime based on logic, CLI flags in the case of the sample.

Implementation sample

Check out code source

Usage

1
package main
2
3
import (
4
"flag"
5
"log"
6
7
"github.com/koltyakov/gosip"
8
"github.com/koltyakov/gosip/api"
9
"github.com/koltyakov/gosip-sandbox/samples/dynauth"
10
)
11
12
func main() {
13
14
strategy := flag.String("strategy", "ondemand", "Auth strategy")
15
config := flag.String("config", "./config/private.json", "Config path")
16
17
flag.Parse()
18
19
authCnfg, err := dynauth.NewAuthCnfg(*strategy, *config)
20
if err != nil {
21
log.Fatalf("unable to get config: %v", err)
22
}
23
24
client := &gosip.SPClient{AuthCnfg: authCnfg}
25
sp := api.NewSP(client)
26
27
// ...
28
29
}
Copied!
When the application should be started with corresponding flags and configuration files on the file system. The sample shows the idea and has a field for improvements, e.g. prompting for credentials, providing site URL within the arguments, using plugins for strategies, and so on.
We plan adding strategies resolver and dynamic environment detection in the future.
Export as PDF
Copy link