github.com/wanliu/go-oauth2-server@v0.0.0-20180817021415-f928fa1580df/oauth/response.go (about)

     1  package oauth
     2  
     3  import (
     4  	"github.com/wanliu/go-oauth2-server/models"
     5  )
     6  
     7  // AccessTokenResponse ...
     8  type AccessTokenResponse struct {
     9  	UserID       string        `json:"user_id,omitempty"`
    10  	User         *UserResponse `json:"user,omitempty"`
    11  	AccessToken  string        `json:"access_token"`
    12  	ExpiresIn    int           `json:"expires_in"`
    13  	TokenType    string        `json:"token_type"`
    14  	Scope        string        `json:"scope"`
    15  	RefreshToken string        `json:"refresh_token,omitempty"`
    16  }
    17  
    18  // IntrospectResponse ...
    19  type IntrospectResponse struct {
    20  	Active       bool          `json:"active"`
    21  	Scope        string        `json:"scope,omitempty"`
    22  	ClientID     string        `json:"client_id,omitempty"`
    23  	Username     string        `json:"username,omitempty"`
    24  	User         *UserResponse `json:"user,omitempty"`
    25  	TokenType    string        `json:"token_type,omitempty"`
    26  	RefreshToken string        `json:"refresh_token"`
    27  	ExpiresAt    int           `json:"exp,omitempty"`
    28  }
    29  
    30  type UserResponse struct {
    31  	ID       string `json:"id"`
    32  	NickName string `json:"nickname"`
    33  	Avatar   string `json:"avatar"`
    34  }
    35  
    36  // NewAccessTokenResponse ...
    37  func NewAccessTokenResponse(accessToken *models.OauthAccessToken, refreshToken *models.OauthRefreshToken, lifetime int, theTokenType string) (*AccessTokenResponse, error) {
    38  	response := &AccessTokenResponse{
    39  		AccessToken: accessToken.Token,
    40  		ExpiresIn:   lifetime,
    41  		TokenType:   theTokenType,
    42  		Scope:       accessToken.Scope,
    43  	}
    44  	if accessToken.User != nil {
    45  		response.UserID = accessToken.User.MetaUserID
    46  		response.User = &UserResponse{
    47  			ID:       accessToken.User.ID,
    48  			NickName: accessToken.User.NickName.String,
    49  			Avatar:   accessToken.User.Avatar(),
    50  		}
    51  	}
    52  	if refreshToken != nil {
    53  		response.RefreshToken = refreshToken.Token
    54  	}
    55  	return response, nil
    56  }