Best Testkube code snippet using oauth.getOAuthConfig
oauth.go
Source:oauth.go
...179 o.RUnlock()180 o.Lock()181 defer o.Unlock()182 wc := wechat.NewWechat()183 appId, err := o.getOAuthConfig(config2.OAuthConfigWechatAppId)184 if err != nil {185 return nil, err186 }187 appSecret, err := o.getOAuthConfig(config2.OAuthConfigWechatAppSecret)188 if err != nil {189 return nil, err190 }191 token, err := o.getOAuthConfig(config2.OAuthConfigWechatToken)192 if err != nil {193 return nil, err194 }195 encodingAESKey, err := o.getOAuthConfig(config2.OAuthConfigWechatEncodingaeskey)196 if err != nil {197 return nil, err198 }199 sysCache := utils.NewWxCache(o.kv)200 of := wc.GetOfficialAccount(&offConfig.Config{201 AppID: appId,202 AppSecret: appSecret,203 Token: token,204 EncodingAESKey: encodingAESKey,205 Cache: sysCache,206 })207 o.officialaccount = of208 } else {209 o.RUnlock()210 }211 return o.officialaccount, nil212}213func (o *oauth) GetWechat() (*WechatConfig, error) {214 reverseProxy, err := o.getOAuthConfig(config2.OAuthConfigWechatReverseProxy)215 if err != nil {216 return nil, err217 }218 wc, err := o.getWechatClient()219 if err != nil {220 return nil, err221 }222 return &WechatConfig{223 Officialaccount: wc,224 ReverseProxy: reverseProxy,225 }, nil226}227func (o *oauth) WechatScanRequest(op string) (*vo.WechatScan, error) {228 client, err := o.getWechatClient()229 if err != nil {230 return nil, err231 }232 code := utils.RandString(16, 1)233 ticket, err := client.GetBasic().GetQRTicket(&basic.Request{234 ExpireSeconds: 600,235 ActionName: "QR_STR_SCENE",236 ActionInfo: struct {237 Scene struct {238 SceneStr string `json:"scene_str,omitempty"`239 SceneID int `json:"scene_id,omitempty"`240 } `json:"scene"`241 }{242 Scene: struct {243 SceneStr string `json:"scene_str,omitempty"`244 SceneID int `json:"scene_id,omitempty"`245 }{246 SceneStr: op + "_" + code,247 },248 },249 })250 if err != nil {251 return nil, err252 }253 return &vo.WechatScan{254 URL: "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket.Ticket,255 Code: code,256 }, nil257}258func (o *oauth) WechatScanLogin(openid, code string) error {259 client, err := o.getWechatClient()260 if err != nil {261 return err262 }263 userinfo, err := client.GetUser().GetUserInfo(openid)264 if err != nil {265 return err266 }267 if userinfo.Subscribe == 0 {268 return errors.New("没æå
³æ³¨å
¬ä¼å·")269 }270 wechat, err := o.wechatOAuthRepo.FindByOpenid(openid)271 if err != nil {272 return err273 }274 var uid int64275 if wechat == nil {276 // æ°å»ºè´¦å·277 user := &entity2.User{278 Username: userinfo.Nickname,279 Role: "user",280 Createtime: time.Now().Unix(),281 Updatetime: time.Now().Unix(),282 }283 if err := o.userSvc.CheckUsername(user.Username); err != nil {284 if err != errs.ErrUsernameExist {285 return err286 }287 user.Username += utils.RandString(4, 2)288 }289 if err := o.tx.Transaction(func(tx *gorm.DB) error {290 uid, err = o.userSvc.oauthRegister(tx, user)291 if err != nil {292 return err293 }294 return persistence.NewWechatOAuth(tx, o.kv).Save(&entity2.WechatOauthUser{295 Openid: openid,296 Unionid: userinfo.UnionID,297 UserID: uid,298 Status: cnt.ACTIVE,299 Createtime: time.Now().Unix(),300 })301 }); err != nil {302 return err303 }304 if userinfo.Headimgurl != "" {305 if b, err := util.HTTPGet(userinfo.Headimgurl); err == nil {306 if err := o.userSvc.UploadAvatar(uid, b); err != nil {307 logrus.Errorf("wechat register upload %s avatar: %v", userinfo.Headimgurl, err)308 }309 } else {310 logrus.Errorf("wechat register download %s avatar: %v", userinfo.Headimgurl, err)311 }312 }313 } else {314 _, err := o.userSvc.UserInfo(wechat.UserID)315 if err != nil {316 return err317 }318 uid = wechat.UserID319 }320 if err := o.wechatOAuthRepo.BindCodeUid(code, uid); err != nil {321 return err322 }323 return client.GetCustomerMessageManager().Send(&message.CustomerMessage{324 ToUser: openid,325 Msgtype: "text",326 Text: &message.MediaText{327 Content: "æ«ç ç»å½æå",328 },329 })330}331func (o *oauth) WechatScanLoginStatus(code string) (*vo.OAuthRespond, error) {332 uid, err := o.wechatOAuthRepo.FindCodeUid(code)333 if err != nil {334 return nil, err335 }336 if err := o.wechatOAuthRepo.DelCode(code); err != nil {337 return nil, err338 }339 user, err := o.userSvc.UserInfo(uid)340 if err != nil {341 return nil, err342 }343 return &vo.OAuthRespond{344 UserInfo: user,345 IsBind: true,346 }, nil347}348func (o *oauth) getBbsClient() (*bbs.Client, error) {349 o.RLock()350 if o.bbs == nil {351 o.RUnlock()352 o.Lock()353 defer o.Unlock()354 clientid, err := o.getOAuthConfig(config2.OAuthConfigBbsClientId)355 if err != nil {356 return nil, err357 }358 clientSecret, err := o.getOAuthConfig(config2.OAuthConfigBbsClientSecret)359 if err != nil {360 return nil, err361 }362 o.bbs = bbs.NewClient(bbs.Config{363 ClientID: clientid,364 ClientSecret: clientSecret,365 })366 } else {367 o.RUnlock()368 }369 return o.bbs, nil370}371func (o *oauth) getOAuthConfig(key string) (string, error) {372 ret, err := o.config.GetConfig(key)373 if err != nil {374 return "", err375 }376 if ret == "" {377 return "", errs.ErrOAuthPlatformNotConfigured378 }379 return ret, nil380}381func (o *oauth) OAuthPlatform(uid int64) (*vo.OpenPlatform, error) {382 ret := &vo.OpenPlatform{383 Bbs: true, Wechat: true,384 }385 var err error...
oauth_client_controller.go
Source:oauth_client_controller.go
1package web2import (3 context2 "context"4 "github.com/jimersylee/go-bbs/services/oauth"5 "github.com/jimersylee/go-bbs/utils/config"6 "github.com/jimersylee/go-bbs/utils/simple"7 "github.com/kataras/iris"8 "github.com/kataras/iris/context"9 "golang.org/x/oauth2"10)11const oauthState = "mlog"12type OauthClientController struct {13 Ctx context.Context14}15// 跳转å°ææ页16func (c *OauthClientController) Any() {17 authCodeUrl := oauth.GetOauthConfig().AuthCodeURL(oauthState)18 c.Ctx.Redirect(authCodeUrl, iris.StatusSeeOther)19}20// åè°å°å21func (c *OauthClientController) AnyCallback() {22 state := c.Ctx.FormValue("state")23 if state != oauthState {24 _, _ = c.Ctx.JSON(simple.ErrorMsg("State invalid"))25 return26 }27 code := c.Ctx.FormValue("code")28 token, err := oauth.GetOauthConfig().Exchange(context2.TODO(), code)29 if err != nil {30 _, _ = c.Ctx.JSON(simple.ErrorMsg("Code exchange failed with " + err.Error()))31 return32 }33 _, _ = c.Ctx.HTML(oauth.GetSuccessHtml(token, config.Conf.OauthClient.ClientSuccessUrl))34}35// éè¿refreshTokenéæ°æ¢åaccessToken36func (c *OauthClientController) AnyRefresh() *simple.JsonResult {37 refreshToken := c.Ctx.FormValue("refreshToken")38 token := &oauth2.Token{RefreshToken: refreshToken}39 ts := oauth.GetOauthConfig().TokenSource(context2.TODO(), token)40 newToken, err := ts.Token()41 if err != nil {42 return simple.ErrorMsg(err.Error())43 }44 return simple.JsonData(newToken)45}...
config.go
Source:config.go
1package oidc2import (3 "github.com/coreos/go-oidc/v3/oidc"4 "golang.org/x/oauth2"5)6type config struct {7 getProvider func() (*oidc.Provider, error)8 getVerifier func(provider *oidc.Provider) *oidc.IDTokenVerifier9 getOauthConfig func(provider *oidc.Provider) *oauth2.Config10}11// An Option customizes the config.12type Option func(*config)13func getConfig(options ...Option) *config {14 cfg := &config{}15 for _, option := range options {16 option(cfg)17 }18 return cfg19}20// WithGetOauthConfig sets the getOauthConfig function in the config.21func WithGetOauthConfig(f func(provider *oidc.Provider) *oauth2.Config) Option {22 return func(cfg *config) {23 cfg.getOauthConfig = f24 }25}26// WithGetProvider sets the getProvider function in the config.27func WithGetProvider(f func() (*oidc.Provider, error)) Option {28 return func(cfg *config) {29 cfg.getProvider = f30 }31}32// WithGetVerifier sets the getVerifier function in the config.33func WithGetVerifier(f func(*oidc.Provider) *oidc.IDTokenVerifier) Option {34 return func(cfg *config) {35 cfg.getVerifier = f36 }37}...
getOAuthConfig
Using AI Code Generation
1func main() {2 oauth.getOAuthConfig()3}4func main() {5 oauth.getOAuthConfig()6}7func main() {8 oauth.getOAuthConfig()9}10func main() {11 oauth.getOAuthConfig()12}13func main() {14 oauth.getOAuthConfig()15}16func main() {17 oauth.getOAuthConfig()18}19func main() {20 oauth.getOAuthConfig()21}22func main() {23 oauth.getOAuthConfig()24}25func main() {26 oauth.getOAuthConfig()27}28func main() {29 oauth.getOAuthConfig()30}31func main() {32 oauth.getOAuthConfig()33}34func main() {35 oauth.getOAuthConfig()36}37func main() {38 oauth.getOAuthConfig()39}40func main() {41 oauth.getOAuthConfig()42}
getOAuthConfig
Using AI Code Generation
1func main() {2 oauth.getOAuthConfig()3}4func main() {5 oauth.getOAuthConfig()6}7func main() {8 oauth.getOAuthConfig()9}10func main() {11 oauth.getOAuthConfig()12}13func main() {14 oauth.getOAuthConfig()15}16func main() {17 oauth.getOAuthConfig()18}19func main() {20 oauth.getOAuthConfig()21}22func main() {23 oauth.getOAuthConfig()24}25func main() {26 oauth.getOAuthConfig()27}28func main() {29 oauth.getOAuthConfig()30}31func main() {32 oauth.getOAuthConfig()33}34func main() {35 oauth.getOAuthConfig()36}
getOAuthConfig
Using AI Code Generation
1import (2func main() {3 config := getOAuthConfig()4 httpCore := httpcore.NewDefaultHttpCore(config)5 client := msgraph.NewGraphServiceClient(config, httpCore)6 user, err := client.Me().Get(nil)7 fmt.Println(user, err)8}9import (10func main() {11 config := getOAuthConfig()12 httpCore := httpcore.NewDefaultHttpCore(config)13 client := msgraph.NewGraphServiceClient(config, httpCore)14 user, err := client.Me().Get(nil)15 fmt.Println(user, err)16}17import (18func main() {19 config := getOAuthConfig()20 httpCore := httpcore.NewDefaultHttpCore(config)21 client := msgraph.NewGraphServiceClient(config, httpCore)22 user, err := client.Me().Get(nil)23 fmt.Println(user, err)24}25import (26func main() {27 oauth.getOAuthConfig()28}29func main() {30 oauth.getOAuthConfig()31}
getOAuthConfig
Using AI Code Generation
1import (2func main() {3 oauthConfig := goloauth.GetOAuthConfig(4 golenv.Vars("GOOGLE_CLIENT_ID"),5 golenv.Vars("GOOGLE_CLIENT_SECRET"),6 fmt.Println(oauthConfig)7}8import (9func main() {10 oauthConfig := goloauth.GetOAuthConfig(11 golenv.Vars("GOOGLE_CLIENT_ID"),12 golenv.Vars("GOOGLE_CLIENT_SECRET"),13 fmt.Println(oauthConfig)14}15import (16func main() {17 oauthConfig := goloauth.GetOAuthConfig(18 golenv.Vars("GOOGLE_CLIENT_ID"),19 golenv.Vars("GOOGLE_CLIENT_SECRET"),20 fmt.Println(oauthConfig)21}22import (23func main() {24 oauthConfig := goloauth.GetOAuthConfig(25 golenv.Vars("GOOGLE_CLIENT_ID"),26 golenv.Vars("GOOGLE_CLIENT_SECRET"),27 fmt.Println(oauthConfig)28}29import (30func main() {31 oauthConfig := goloauth.GetOAuthConfig(32 golenv.Vars("GOOGLE_CLIENT_ID"),33 golenv.Vars("GOOGLE_CLIENT_SECRET"),
getOAuthConfig
Using AI Code Generation
1import (2func main() {3 config := getOAuthConfig()4 httpCore := httpcore.NewDefaultHttpCore(config)5 client := msgraph.NewGraphServiceClient(config, httpCore)6 user, err := client.Me().Get(nil)7 fmt.Println(user, err)8}9import (10func main() {11 config := getOAuthConfig()12 httpCore := httpcore.NewDefaultHttpCore(config)13 client := msgraph.NewGraphServiceClient(config, httpCore)14 user, err := client.Me().Get(nil)15 fmt.Println(user, err)16}17import (18func main() {19 config := getOAuthConfig()20 httpCore := httpcore.NewDefaultHttpCore(config)21 client := msgraph.NewGraphServiceClient(config, httpCore)22 user, err := client.Me().Get(nil)23 fmt.Println(user, err)24}25import (
getOAuthConfig
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 oauth := oauth.NewOAuth()5 oauth.GetOAuthConfig()6}7import (8type OAuth struct {9}10func NewOAuth() *OAuth {11 return &OAuth{}12}13func (oauth *OAuth) GetOAuthConfig() {14 fmt.Println("Hello, playground")15}16 /usr/local/go/src/pkg/oauth (from $GOROOT)17 /Users/username/Documents/go/src/oauth (from $GOPATH)
getOAuthConfig
Using AI Code Generation
1import (2func main() {3 golenv.Load()4 oauth := NewOAuth("twitter")5 fmt.Println(oauth.getOAuthConfig())6}7import (8func main() {9 golenv.Load()10 oauth := NewOAuth("google")11 fmt.Println(oauth.getOAuthConfig())12}13import (14func main() {15 golenv.Load()16 oauth := NewOAuth("facebook")17 fmt.Println(oauth.getOAuthConfig())18}19import (20func main() {21 golenv.Load()22 oauth := NewOAuth("linkedin")23 fmt.Println(oauth.getOAuthConfig())24}25import (26func main() {27 golenv.Load()28 oauth := NewOAuth("github")29 fmt.Println(oauth.getOAuthConfig())30}31import (32func main() {33 golenv.Load()34 oauth := NewOAuth("dropbox")35 fmt.Println(oauth.getOAuthConfig())36}37import (38func main() {39 golenv.Load()40 oauth := NewOAuth("instagram")41 fmt.Println(oauth.getOAuthConfig())42}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!