Skip to content

Commit

Permalink
move library to own repo
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmann committed Apr 8, 2021
0 parents commit 119e576
Show file tree
Hide file tree
Showing 13 changed files with 1,202 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version = 1

[[analyzers]]
name = "go"
enabled = true

[analyzers.meta]
import_paths = ["github.com/oidc-mytoken/lib"]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
tags
dist/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-2021 Gabriel Zachmann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
![mytoken logo](https://git.scc.kit.edu/oidc/mytoken/-/raw/master/docs/img/mytoken.png)

[![License](https://img.shields.io/github/license/oidc-mytoken/lib.svg)](https://github.com/oidc-mytoken/lib/blob/master/LICENSE)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/oidc-mytoken/lib)
[![Go Report](https://goreportcard.com/badge/github.com/oidc-mytoken/lib)](https://goreportcard.com/report/github.com/oidc-mytoken/lib)
[![DeepSource](https://deepsource.io/gh/oidc-mytoken/lib.svg/?label=active+issues&show_trend=true)](https://deepsource.io/gh/oidc-mytoken/lib/?ref=repository-badge)
[![Release date](https://img.shields.io/github/release-date/oidc-mytoken/lib.svg)](https://github.com/oidc-mytoken/lib/releases/latest)
[![Release version](https://img.shields.io/github/release/oidc-mytoken/lib.svg)](https://github.com/oidc-mytoken/lib/releases/latest)

# mytokenlib

`mytokenlib` is a go library for communicating with amytoken server.
`mytoken` is a central web service with the goal to easily obtain OpenID Connect access tokens across devices.

A mytoken command line client can be found at [https://github.com/oidc-mytoken/client](https://github.com/oidc-mytoken/client).

The mytoken server can be found at [https://github.com/oidc-mytoken/server](https://github.com/oidc-mytoken/server).

A demo instance of mytoken is running at [https://mytoken.data.kit.edu/](https://mytoken.data.kit.edu/).
29 changes: 29 additions & 0 deletions accesstoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mytokenlib

import (
"github.com/oidc-mytoken/server/pkg/api/v0"
"github.com/oidc-mytoken/server/shared/httpClient"
)

func (my *MytokenProvider) GetAccessToken(mytoken, oidcIssuer string, scopes, audiences []string, comment string) (string, error) {
req := NewAccessTokenRequest(oidcIssuer, mytoken, scopes, audiences, comment)
resp, err := httpClient.Do().R().SetBody(req).SetResult(&api.AccessTokenResponse{}).SetError(&api.APIError{}).Post(my.AccessTokenEndpoint)
if err != nil {
return "", newMytokenErrorFromError("error while sending http request", err)
}
if e := resp.Error(); e != nil {
if errRes := e.(*api.APIError); errRes != nil && errRes.Error != "" {
return "", &MytokenError{
err: errRes.Error,
errorDetails: errRes.ErrorDescription,
}
}
}
atRes, ok := resp.Result().(*api.AccessTokenResponse)
if !ok {
return "", &MytokenError{
err: unexpectedResponse,
}
}
return atRes.AccessToken, nil
}
25 changes: 25 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 err.errorDetails != "" {
e += ": " + err.errorDetails
}
return e
}

func newMytokenErrorFromError(e string, err error) *MytokenError {
return &MytokenError{
err: e,
errorDetails: err.Error(),
}
}

const unexpectedResponse = "unexpected response from mytoken server"
const errorWhileHttp = "error while sending http request"
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/oidc-mytoken/lib

go 1.13

require github.com/oidc-mytoken/server v0.1.1-0.20210408094857-353b733d8e2b
Loading

0 comments on commit 119e576

Please sign in to comment.