-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature/user api (#1) * Init user api * Add first method * Add ClientTokenSource interface as an abstract layer of api token provider * Add request interface * Fix readme * fix token url * fix oauth * fix interface * fix tokensource * add authen api * fix test * fix test * fix time * fix test * fix app * fix token * add Type * add lark supports * fix readme * change feishu -> lafi
- Loading branch information
Showing
18 changed files
with
477 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package auth | ||
|
||
import "github.com/joyqi/go-lafi/api" | ||
|
||
const ( | ||
AppCommonURL = "/auth/v3/app_access_token" | ||
AppInternalURL = "/auth/v3/app_access_token/internal" | ||
) | ||
|
||
// AppCommonBody represents a request to retrieve an app token | ||
type AppCommonBody struct { | ||
AppID string `json:"app_id"` | ||
AppSecret string `json:"app_secret"` | ||
AppTicket string `json:"app_ticket"` | ||
} | ||
|
||
// AppInternalBody represents a request to retrieve an app token | ||
type AppInternalBody struct { | ||
AppID string `json:"app_id"` | ||
AppSecret string `json:"app_secret"` | ||
} | ||
|
||
type App api.Api | ||
|
||
// CommonAccessToken retrieves a common token from the app token endpoint | ||
func (a *App) CommonAccessToken(body *AppCommonBody) (string, int64, error) { | ||
return MakeTokenApi(a, "app_access_token", AppCommonURL, body) | ||
} | ||
|
||
// InternalAccessToken retrieves an internal token from the app token endpoint | ||
func (a *App) InternalAccessToken(body *AppInternalBody) (string, int64, error) { | ||
return MakeTokenApi(a, "app_access_token", AppInternalURL, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/joyqi/go-lafi/api" | ||
) | ||
|
||
// TokenResponse represents the common token response structure | ||
type TokenResponse struct { | ||
// Code is the response status code | ||
Code int `json:"code"` | ||
|
||
// Msg is the response message | ||
Msg string `json:"msg"` | ||
|
||
// AccessToken is the access token | ||
AccessToken string | ||
|
||
// Expire is the expiration time of the access token | ||
Expire int64 `json:"expire"` | ||
} | ||
|
||
// MakeTokenApi creates a new token api | ||
func MakeTokenApi(c api.Client, tokenName string, uri string, body interface{}) (string, int64, error) { | ||
var resp map[string]json.RawMessage | ||
token := TokenResponse{} | ||
err := c.Request("POST", uri, body, &resp) | ||
|
||
if err = json.Unmarshal(resp["code"], &token.Code); err != nil { | ||
return "", 0, err | ||
} | ||
|
||
if err = json.Unmarshal(resp["msg"], &token.Msg); err != nil { | ||
return "", 0, err | ||
} | ||
|
||
if err = json.Unmarshal(resp[tokenName], &token.AccessToken); err != nil { | ||
return "", 0, err | ||
} | ||
|
||
if err = json.Unmarshal(resp["expire"], &token.Expire); err != nil { | ||
return "", 0, err | ||
} | ||
|
||
if token.Code != 0 { | ||
return "", 0, errors.New(token.Msg) | ||
} | ||
|
||
return token.AccessToken, token.Expire, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package auth | ||
|
||
import "github.com/joyqi/go-lafi/api" | ||
|
||
const ( | ||
TenantCommonURL = "/auth/v3/tenant_access_token" | ||
TenantInternalURL = "/auth/v3/tenant_access_token/internal" | ||
) | ||
|
||
// TenantCommonBody represents a request to retrieve a tenant token | ||
type TenantCommonBody struct { | ||
AppAccessToken string `json:"app_access_token"` | ||
TenantKey string `json:"tenant_key"` | ||
} | ||
|
||
// TenantInternalBody represents a request to retrieve a tenant token | ||
type TenantInternalBody struct { | ||
AppID string `json:"app_id"` | ||
AppSecret string `json:"app_secret"` | ||
} | ||
|
||
type Tenant api.Api | ||
|
||
// CommonAccessToken retrieves a common token from the tenant token endpoint | ||
func (t *Tenant) CommonAccessToken(body *TenantCommonBody) (string, int64, error) { | ||
return MakeTokenApi(t, "tenant_access_token", TenantCommonURL, body) | ||
} | ||
|
||
// InternalAccessToken retrieves a internal token from the tenant token endpoint | ||
func (t *Tenant) InternalAccessToken(body *TenantInternalBody) (string, int64, error) { | ||
return MakeTokenApi(t, "tenant_access_token", TenantInternalURL, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package authen | ||
|
||
import ( | ||
"github.com/joyqi/go-lafi/api" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
AccessTokenURL = "/authen/v1/access_token" | ||
AccessTokenRefreshURL = "/authen/v1/refresh_access_token" | ||
) | ||
|
||
// AccessTokenCreateBody represents the request body of creating AccessToken | ||
type AccessTokenCreateBody struct { | ||
GrantType string `json:"grant_type"` | ||
Code string `json:"code"` | ||
} | ||
|
||
// AccessTokenData represents the data of creating AccessToken | ||
type AccessTokenData struct { | ||
// OpenId represents the open ID of the user | ||
OpenId string `json:"open_id"` | ||
|
||
// AccessToken is the token used to access the application | ||
AccessToken string `json:"access_token"` | ||
|
||
// RefreshToken is the token used to refresh the user's access token | ||
RefreshToken string `json:"refresh_token"` | ||
|
||
// ExpiresIn is the number of seconds the token will be valid | ||
ExpiresIn int64 `json:"expires_in"` | ||
} | ||
|
||
// AccessTokenRefreshBody represents the request body of refreshing AccessToken | ||
type AccessTokenRefreshBody struct { | ||
GrantType string `json:"grant_type"` | ||
RefreshToken string `json:"refresh_token"` | ||
} | ||
|
||
type AccessToken api.Api | ||
|
||
// Create creates the access token. | ||
func (a *AccessToken) Create(body *AccessTokenCreateBody) (*AccessTokenData, error) { | ||
return api.MakeApi[AccessTokenData](a.Client, http.MethodPost, AccessTokenURL, body) | ||
} | ||
|
||
// Refresh refreshes the access token. | ||
func (a *AccessToken) Refresh(body *AccessTokenRefreshBody) (*AccessTokenData, error) { | ||
return api.MakeApi[AccessTokenData](a.Client, http.MethodPost, AccessTokenRefreshURL, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package authen | ||
|
||
import ( | ||
"github.com/joyqi/go-lafi/api" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
UserInfoURL = "/authen/v1/user_info" | ||
) | ||
|
||
// UserInfoData represents the response data of UserInfo | ||
type UserInfoData struct { | ||
Name string `json:"name"` | ||
EnName string `json:"en_name"` | ||
AvatarURL string `json:"avatar_url"` | ||
AvatarThumb string `json:"avatar_thumb"` | ||
AvatarMiddle string `json:"avatar_middle"` | ||
AvatarBig string `json:"avatar_big"` | ||
OpenId string `json:"open_id"` | ||
UnionId string `json:"union_id"` | ||
Email string `json:"email"` | ||
EnterpriseEmail string `json:"enterprise_email"` | ||
UserId string `json:"user_id"` | ||
Mobile string `json:"mobile"` | ||
TenantKey string `json:"tenant_key"` | ||
} | ||
|
||
type UserInfo api.Api | ||
|
||
// Get fetches the user info through the access token. | ||
func (a *UserInfo) Get() (data *UserInfoData, err error) { | ||
return api.MakeApi[UserInfoData](a.Client, http.MethodGet, UserInfoURL, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.