http

package
v0.0.0-...-026650a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2024 License: ISC Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GithubUserFetcher = func(ctx context.Context, config *oauth2.Config, token *oauth2.Token) (nakama.ProvidedUser, error) {
	const baseURL = "https://api.github.com"

	var user struct {
		ID    int    `json:"id"`
		Login string `json:"login"`
	}

	var emails []struct {
		Email    string `json:"email"`
		Primary  bool   `json:"primary"`
		Verified bool   `json:"verified"`
	}

	g, gctx := errgroup.WithContext(ctx)

	g.Go(func() error {
		req, err := http.NewRequestWithContext(gctx, http.MethodGet, baseURL+"/user", nil)
		if err != nil {
			return fmt.Errorf("could not create user request: %w", err)
		}

		req.Header.Set("User-Agent", "nakama")

		resp, err := config.Client(ctx, token).Do(req)
		if err != nil {
			return errServiceUnavailable
		}

		defer resp.Body.Close()

		if resp.StatusCode >= 400 {
			return errServiceUnavailable
		}

		err = json.NewDecoder(resp.Body).Decode(&user)
		if err != nil {
			return errServiceUnavailable
		}

		return nil
	})

	g.Go(func() error {
		req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/user/emails", nil)
		if err != nil {
			return fmt.Errorf("could not create user emails request: %w", err)
		}

		req.Header.Set("User-Agent", "nakama")

		resp, err := config.Client(ctx, token).Do(req)
		if err != nil {
			return errServiceUnavailable
		}

		defer resp.Body.Close()

		if resp.StatusCode >= 400 {
			return errServiceUnavailable
		}

		err = json.NewDecoder(resp.Body).Decode(&emails)
		if err != nil {
			return errServiceUnavailable
		}

		return nil
	})

	var out nakama.ProvidedUser

	if err := g.Wait(); err != nil {
		return out, err
	}

	for _, email := range emails {
		if email.Email != "" && email.Verified && email.Primary {
			out.Email = email.Email
			break
		}
	}

	if out.Email == "" {
		for _, email := range emails {
			if email.Email != "" && email.Verified {
				out.Email = email.Email
				break
			}
		}
	}

	if out.Email == "" {
		return out, errEmailNotProvided
	}

	out.ID = fmt.Sprintf("%d", user.ID)
	out.Username = &user.Login

	return out, nil
}

Functions

func New

func New(svc transport.Service, oauthProviders []OauthProvider, origin *url.URL, logger log.Logger, store storage.Store, cdc *securecookie.SecureCookie, promHandler http.Handler, embedStaticFiles bool) http.Handler

New makes use of the service to provide an http.Handler with predefined routing.

Types

type OauthProvider

type OauthProvider struct {
	Name            string
	Config          *oauth2.Config
	FetchUser       func(ctx context.Context, config *oauth2.Config, token *oauth2.Token) (nakama.ProvidedUser, error)
	IDTokenVerifier *oidc.IDTokenVerifier
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL