Skip to content
Snippets Groups Projects
Commit c0a5d1c4 authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

restructure + client POC

parent 91b42487
No related branches found
No related tags found
No related merge requests found
Showing
with 149 additions and 15 deletions
/mytoken /mytoken
/.idea/ /.idea/
/setup /setup
/client
tags tags
client.config client.config
config/server/config.yaml config/server/config.yaml
......
package main package main
import ( import (
"fmt"
"os"
"github.com/zachmann/mytoken/internal/client/config"
"github.com/zachmann/mytoken/internal/httpClient" "github.com/zachmann/mytoken/internal/httpClient"
"github.com/zachmann/mytoken/pkg/mytokenlib"
) )
func main() { func main() {
config.Init()
httpClient.Init("") httpClient.Init("")
mytoken, err := mytokenlib.NewMytokenInstance(config.Get().URL)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
st := os.Getenv("ST_AT")
at, err := mytoken.GetAccessToken(st, nil, nil, "testAT")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(at)
} }
package config
type config struct {
URL string `yaml:"url"`
}
var conf *config
// Get returns the config
func Get() *config {
return conf
}
func Init() {
conf = &config{
URL: "http://localhost:8000",
}
}
...@@ -10,11 +10,10 @@ import ( ...@@ -10,11 +10,10 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/zachmann/mytoken/internal/model"
"github.com/zachmann/mytoken/internal/context" "github.com/zachmann/mytoken/internal/context"
"github.com/zachmann/mytoken/internal/server/utils/issuerUtils" "github.com/zachmann/mytoken/internal/server/utils/issuerUtils"
"github.com/zachmann/mytoken/internal/utils/fileutil" "github.com/zachmann/mytoken/internal/utils/fileutil"
"github.com/zachmann/mytoken/pkg/model"
"github.com/zachmann/mytoken/pkg/oauth2x" "github.com/zachmann/mytoken/pkg/oauth2x"
) )
......
...@@ -7,10 +7,9 @@ import ( ...@@ -7,10 +7,9 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/zachmann/mytoken/internal/model"
"github.com/zachmann/mytoken/internal/server/db" "github.com/zachmann/mytoken/internal/server/db"
"github.com/zachmann/mytoken/internal/server/db/dbrepo/authcodeinforepo/state" "github.com/zachmann/mytoken/internal/server/db/dbrepo/authcodeinforepo/state"
"github.com/zachmann/mytoken/pkg/model"
) )
// TransferCodeStatus holds information about the status of a polling code // TransferCodeStatus holds information about the status of a polling code
......
...@@ -7,10 +7,9 @@ import ( ...@@ -7,10 +7,9 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/zachmann/mytoken/internal/model"
"github.com/zachmann/mytoken/internal/server/config" "github.com/zachmann/mytoken/internal/server/config"
"github.com/zachmann/mytoken/internal/server/db" "github.com/zachmann/mytoken/internal/server/db"
"github.com/zachmann/mytoken/pkg/model"
) )
// TransferCode is a type used to transfer a token // TransferCode is a type used to transfer a token
......
package pkg package pkg
import "github.com/zachmann/mytoken/internal/model" import "github.com/zachmann/mytoken/pkg/model"
// MytokenConfiguration holds information about a mytoken instance // MytokenConfiguration holds information about a mytoken instance
type MytokenConfiguration struct { type MytokenConfiguration struct {
......
package pkg package pkg
import ( import (
"github.com/zachmann/mytoken/internal/model"
"github.com/zachmann/mytoken/internal/server/supertoken/token" "github.com/zachmann/mytoken/internal/server/supertoken/token"
"github.com/zachmann/mytoken/pkg/model"
) )
// AccessTokenRequest holds an request for an access token // AccessTokenRequest holds an request for an access token
......
...@@ -3,6 +3,8 @@ package model ...@@ -3,6 +3,8 @@ package model
import ( import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"github.com/zachmann/mytoken/pkg/model"
) )
// Response models a http server response // Response models a http server response
...@@ -32,7 +34,7 @@ func (r Response) Send(ctx *fiber.Ctx) error { ...@@ -32,7 +34,7 @@ func (r Response) Send(ctx *fiber.Ctx) error {
func ErrorToInternalServerErrorResponse(err error) *Response { func ErrorToInternalServerErrorResponse(err error) *Response {
return &Response{ return &Response{
Status: fiber.StatusInternalServerError, Status: fiber.StatusInternalServerError,
Response: InternalServerError(err.Error()), Response: model.InternalServerError(err.Error()),
} }
} }
...@@ -40,6 +42,9 @@ func ErrorToInternalServerErrorResponse(err error) *Response { ...@@ -40,6 +42,9 @@ func ErrorToInternalServerErrorResponse(err error) *Response {
func ErrorToBadRequestErrorResponse(err error) *Response { func ErrorToBadRequestErrorResponse(err error) *Response {
return &Response{ return &Response{
Status: fiber.StatusBadRequest, Status: fiber.StatusBadRequest,
Response: BadRequestError(err.Error()), Response: model.BadRequestError(err.Error()),
} }
} }
// ResponseNYI is the server response when something is not yet implemented
var ResponseNYI = Response{Status: fiber.StatusNotImplemented, Response: model.APIErrorNYI}
...@@ -3,8 +3,6 @@ package model ...@@ -3,8 +3,6 @@ package model
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gofiber/fiber/v2"
) )
// APIError is an error object that is returned on the api when an error occurs // APIError is an error object that is returned on the api when an error occurs
...@@ -13,9 +11,6 @@ type APIError struct { ...@@ -13,9 +11,6 @@ type APIError struct {
ErrorDescription string `json:"error_description,omitempty"` ErrorDescription string `json:"error_description,omitempty"`
} }
// ResponseNYI is the server response when something is not yet implemented
var ResponseNYI = Response{Status: fiber.StatusNotImplemented, Response: APIErrorNYI}
// Predefined errors // Predefined errors
var ( var (
APIErrorUnknownIssuer = APIError{ErrorInvalidRequest, "The provided issuer is not supported"} APIErrorUnknownIssuer = APIError{ErrorInvalidRequest, "The provided issuer is not supported"}
......
File moved
File moved
File moved
package mytokenlib
import (
"strings"
"github.com/zachmann/mytoken/internal/httpClient"
"github.com/zachmann/mytoken/internal/server/endpoints/token/access/pkg"
"github.com/zachmann/mytoken/internal/server/supertoken/token"
"github.com/zachmann/mytoken/pkg/model"
)
func (my *Mytoken) GetAccessToken(superToken string, scopes []string, audiences []string, comment string) (string, error) {
req := pkg.AccessTokenRequest{
GrantType: model.GrantTypeSuperToken,
SuperToken: token.Token(superToken),
Scope: strings.Join(scopes, " "),
Audience: strings.Join(audiences, " "),
Comment: comment,
}
resp, err := httpClient.Do().R().SetBody(req).SetResult(&pkg.AccessTokenResponse{}).SetError(&model.APIError{}).Post(my.AccessTokenEndpoint)
if err != nil {
return "", newMytokenErrorFromError("error while sending http request", err)
}
if e := resp.Error(); e != nil {
if errRes := e.(*model.APIError); errRes != nil && len(errRes.Error) > 0 {
return "", &MytokenError{
err: errRes.Error,
errorDetails: errRes.ErrorDescription,
}
}
}
atRes, ok := resp.Result().(*pkg.AccessTokenResponse)
if !ok {
return "", &MytokenError{
err: "unexpected response from mytoken server",
}
}
return atRes.AccessToken, nil
}
package mytokenlib
// MytokenError is a error type from the mytoken library
type MytokenError struct {
err string
errorDetails string
}
func (err *MytokenError) Error() string {
e := err.err
if len(err.errorDetails) > 0 {
e += ": " + err.errorDetails
}
return e
}
func newMytokenErrorFromError(e string, err error) *MytokenError {
return &MytokenError{
err: e,
errorDetails: err.Error(),
}
}
package mytokenlib
import (
"github.com/zachmann/mytoken/internal/httpClient"
"github.com/zachmann/mytoken/internal/server/endpoints/configuration/pkg"
"github.com/zachmann/mytoken/internal/utils"
"github.com/zachmann/mytoken/pkg/model"
)
type Mytoken struct {
pkg.MytokenConfiguration
}
func NewMytokenInstance(url string) (*Mytoken, error) {
configEndpoint := utils.CombineURLPath(url, "/.well-known/mytoken-configuration")
resp, err := httpClient.Do().R().SetResult(&pkg.MytokenConfiguration{}).SetError(&model.APIError{}).Get(configEndpoint)
if err != nil {
return nil, newMytokenErrorFromError("could not connect to mytoken instance", err)
}
if e := resp.Error(); e != nil {
if errRes := e.(*model.APIError); errRes != nil && len(errRes.Error) > 0 {
return nil, &MytokenError{
err: errRes.Error,
errorDetails: errRes.ErrorDescription,
}
}
}
config, ok := resp.Result().(*pkg.MytokenConfiguration)
if !ok {
return nil, &MytokenError{
err: "unexpected response from mytoken server",
}
}
return &Mytoken{
MytokenConfiguration: *config,
}, nil
}
package mytokenlib
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment