Skip to content

Commit 119e576

Browse files
committed
move library to own repo
0 parents  commit 119e576

File tree

13 files changed

+1202
-0
lines changed

13 files changed

+1202
-0
lines changed

.deepsource.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version = 1
2+
3+
[[analyzers]]
4+
name = "go"
5+
enabled = true
6+
7+
[analyzers.meta]
8+
import_paths = ["github.com/oidc-mytoken/lib"]

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea/
2+
tags
3+
dist/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020-2021 Gabriel Zachmann
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
![mytoken logo](https://git.scc.kit.edu/oidc/mytoken/-/raw/master/docs/img/mytoken.png)
2+
3+
[![License](https://img.shields.io/github/license/oidc-mytoken/lib.svg)](https://github.com/oidc-mytoken/lib/blob/master/LICENSE)
4+
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/oidc-mytoken/lib)
5+
[![Go Report](https://goreportcard.com/badge/github.com/oidc-mytoken/lib)](https://goreportcard.com/report/github.com/oidc-mytoken/lib)
6+
[![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)
7+
[![Release date](https://img.shields.io/github/release-date/oidc-mytoken/lib.svg)](https://github.com/oidc-mytoken/lib/releases/latest)
8+
[![Release version](https://img.shields.io/github/release/oidc-mytoken/lib.svg)](https://github.com/oidc-mytoken/lib/releases/latest)
9+
10+
# mytokenlib
11+
12+
`mytokenlib` is a go library for communicating with amytoken server.
13+
`mytoken` is a central web service with the goal to easily obtain OpenID Connect access tokens across devices.
14+
15+
A mytoken command line client can be found at [https://github.com/oidc-mytoken/client](https://github.com/oidc-mytoken/client).
16+
17+
The mytoken server can be found at [https://github.com/oidc-mytoken/server](https://github.com/oidc-mytoken/server).
18+
19+
A demo instance of mytoken is running at [https://mytoken.data.kit.edu/](https://mytoken.data.kit.edu/).

accesstoken.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package mytokenlib
2+
3+
import (
4+
"github.com/oidc-mytoken/server/pkg/api/v0"
5+
"github.com/oidc-mytoken/server/shared/httpClient"
6+
)
7+
8+
func (my *MytokenProvider) GetAccessToken(mytoken, oidcIssuer string, scopes, audiences []string, comment string) (string, error) {
9+
req := NewAccessTokenRequest(oidcIssuer, mytoken, scopes, audiences, comment)
10+
resp, err := httpClient.Do().R().SetBody(req).SetResult(&api.AccessTokenResponse{}).SetError(&api.APIError{}).Post(my.AccessTokenEndpoint)
11+
if err != nil {
12+
return "", newMytokenErrorFromError("error while sending http request", err)
13+
}
14+
if e := resp.Error(); e != nil {
15+
if errRes := e.(*api.APIError); errRes != nil && errRes.Error != "" {
16+
return "", &MytokenError{
17+
err: errRes.Error,
18+
errorDetails: errRes.ErrorDescription,
19+
}
20+
}
21+
}
22+
atRes, ok := resp.Result().(*api.AccessTokenResponse)
23+
if !ok {
24+
return "", &MytokenError{
25+
err: unexpectedResponse,
26+
}
27+
}
28+
return atRes.AccessToken, nil
29+
}

error.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package mytokenlib
2+
3+
// MytokenError is a error type from the mytoken library
4+
type MytokenError struct {
5+
err string
6+
errorDetails string
7+
}
8+
9+
func (err *MytokenError) Error() string {
10+
e := err.err
11+
if err.errorDetails != "" {
12+
e += ": " + err.errorDetails
13+
}
14+
return e
15+
}
16+
17+
func newMytokenErrorFromError(e string, err error) *MytokenError {
18+
return &MytokenError{
19+
err: e,
20+
errorDetails: err.Error(),
21+
}
22+
}
23+
24+
const unexpectedResponse = "unexpected response from mytoken server"
25+
const errorWhileHttp = "error while sending http request"

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/oidc-mytoken/lib
2+
3+
go 1.13
4+
5+
require github.com/oidc-mytoken/server v0.1.1-0.20210408094857-353b733d8e2b

0 commit comments

Comments
 (0)