NewAPIWithVersion creates a new REST API using the specified version
(globalConfig *conf.GlobalConfiguration, db *storage.Connection, version string, opt ...Option)
| 96 | |
| 97 | // NewAPIWithVersion creates a new REST API using the specified version |
| 98 | func NewAPIWithVersion(globalConfig *conf.GlobalConfiguration, db *storage.Connection, version string, opt ...Option) *API { |
| 99 | api := &API{ |
| 100 | config: globalConfig, |
| 101 | db: db, |
| 102 | version: version, |
| 103 | } |
| 104 | |
| 105 | api.oidcCache = provider.NewOIDCProviderCache(globalConfig.External.OIDCProviderCacheTTL) |
| 106 | |
| 107 | for _, o := range opt { |
| 108 | o.apply(api) |
| 109 | } |
| 110 | if api.captchaVerifier == nil { |
| 111 | api.captchaVerifier = security.NewCaptchaVerifier(&globalConfig.Security.Captcha) |
| 112 | } |
| 113 | if api.limiterOpts == nil { |
| 114 | api.limiterOpts = apilimiter.New(globalConfig) |
| 115 | } |
| 116 | if api.hooksMgr == nil { |
| 117 | httpDr := hookshttp.New() |
| 118 | pgfuncDr := hookspgfunc.New(db) |
| 119 | api.hooksMgr = v0hooks.NewManager(globalConfig, httpDr, pgfuncDr) |
| 120 | } |
| 121 | |
| 122 | // Initialize token service if not provided via options |
| 123 | if api.tokenService == nil { |
| 124 | api.tokenService = tokens.NewService(globalConfig, api.hooksMgr) |
| 125 | } |
| 126 | if api.mailer == nil { |
| 127 | tc := templatemailer.NewCache() |
| 128 | api.mailer = templatemailer.FromConfig(globalConfig, tc) |
| 129 | } |
| 130 | |
| 131 | // Connect token service to API's time function (supports test overrides) |
| 132 | api.tokenService.SetTimeFunc(api.Now) |
| 133 | |
| 134 | // Initialize OAuth server (only if enabled) |
| 135 | if globalConfig.OAuthServer.Enabled { |
| 136 | api.oauthServer = oauthserver.NewServer(globalConfig, db, api.tokenService) |
| 137 | } |
| 138 | |
| 139 | if api.config.Password.HIBP.Enabled { |
| 140 | httpClient := &http.Client{ |
| 141 | // all HIBP API requests should finish quickly to avoid |
| 142 | // unnecessary slowdowns |
| 143 | Timeout: 5 * time.Second, |
| 144 | } |
| 145 | |
| 146 | api.hibpClient = &hibp.PwnedClient{ |
| 147 | UserAgent: api.config.Password.HIBP.UserAgent, |
| 148 | HTTP: httpClient, |
| 149 | } |
| 150 | |
| 151 | if api.config.Password.HIBP.Bloom.Enabled { |
| 152 | cache := utilities.NewHIBPBloomCache(api.config.Password.HIBP.Bloom.Items, api.config.Password.HIBP.Bloom.FalsePositives) |
| 153 | api.hibpClient.Cache = cache |
| 154 | |
| 155 | logrus.Infof("Pwned passwords cache is %.2f KB", float64(cache.Cap())/(8*1024.0)) |
searching dependent graphs…