oauth2/client/token.go

51 lines
1.1 KiB
Go

package client
import (
"fmt"
"net/url"
"somehole.com/service/oauth2/session"
)
type TokenUrl struct {
*Client
*session.Session
TokenChoice session.TokenChoice
}
func NewTokenUrl(client *Client, id session.SessionId, choice session.TokenChoice) (url *TokenUrl, err error) {
ses, ok := client.sessions[id]
if !ok {
err = fmt.Errorf("no session found")
}
url = &TokenUrl{
Client: client,
Session: ses,
TokenChoice: choice,
}
return
}
func (t *TokenUrl) Url() *url.URL {
v := url.Values{}
if t.TokenChoice == session.TokenChoiceAccess {
v.Set("grant_type", "authorization_code")
v.Set("code", string(t.Code))
v.Set("redirect_uri", string(t.RedirectUri))
} else if t.TokenChoice == session.TokenChoiceRefresh {
v.Set("grant_type", "refresh_token")
v.Set("refresh_token", string(t.RefreshToken))
}
return &url.URL{
Scheme: "https",
User: url.UserPassword(t.ClientId, t.ClientSecret),
Host: t.Host,
Path: t.TokenUrlPath,
RawQuery: v.Encode(),
}
}
func (t *TokenUrl) String() string {
return t.Url().String()
}