├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ ├── build.yml │ └── docker.yml ├── .gitignore ├── .project ├── .vscode └── launch.json ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── README.md ├── _config.yml ├── build.sh ├── compresstoken ├── compressToken.go ├── compressToken_test.go └── coverage.out ├── coverage.out ├── entrypoint.sh ├── go.mod ├── go.sum ├── go.sum754055166.tmp ├── handlers ├── alowedUriRestHandlers.go ├── alowedUriRestHandlers_test.go ├── clientRestHandlers.go ├── clientRestHandlers_test.go ├── coverage.out ├── grantTypeRestHandlers.go ├── grantTypeRestHandlers_test.go ├── handlers.go ├── loggingHandler.go ├── loggingHandler_test.go ├── oauthHandlers.go ├── oauthHandlers_test.go ├── redirectUriRestHandlers.go ├── redirectUriRestHandlers_test.go ├── roleRestHandlers.go ├── roleRestHandlers_test.go ├── roleUriRestHandlers.go ├── roleUriRestHandlers_test.go ├── testHtmls │ └── test.html ├── utilites.go ├── utilitesMock.go ├── utilitesMock_test.go ├── utilites_test.go ├── validateTokenRestHandlers.go ├── validateTokenRestHandlers_test.go ├── webAuthorizationHandlers.go ├── webAuthorizationHandlers_test.go ├── webIndexHandlers.go ├── webIndexHandlers_test.go ├── webLoginHandlers.go ├── webLoginHandlers_test.go ├── webOauthHandlers.go ├── webTokenHandlers.go └── webTokenHandlers_test.go ├── main ├── managers ├── accessTokenManager.go ├── accessTokenManager_test.go ├── authorizationManager.go ├── authorizationManager_test.go ├── clientAllowedUriManager.go ├── clientAllowedUriManager_test.go ├── clientManager.go ├── clientManager_test.go ├── clientRedirectUriManager.go ├── clientRedirectUriManager_test.go ├── clientRoleManager.go ├── clientRoleManager_test.go ├── clientRoleUriManager.go ├── clientRoleUriManager_test.go ├── coverage.out ├── grantTypeManager.go ├── grantTypeManager_test.go ├── implicitManager.go ├── implicitManager_test.go ├── jwt.go ├── jwt_test.go ├── loginManager.go ├── loginManager_integration_test.go ├── loginManager_test.go ├── manager.go ├── mockManager.go ├── mockManager_test.go ├── refreshTokenManager.go ├── refreshTokenManager_test.go ├── runinttest.sh ├── tokenManager.go ├── tokenManager_test.go ├── utilities.go ├── utilities_test.go ├── validationTokenManager.go └── validationTokenManager_test.go ├── mysqldb ├── accessToken.go ├── accessToken_integration_test.go ├── accessToken_test.go ├── allowedUri.go ├── allowedUri_integration_test.go ├── allowedUri_test.go ├── authCodeRevolk.go ├── authCodeRevolk_integration_test.go ├── authCodeRevolk_test.go ├── authCodeScope.go ├── authorizationCode.go ├── authorizationCode_integration_test.go ├── authorizationCode_test.go ├── client.go ├── client2_test.go ├── client3_test.go ├── clientGrantType.go ├── clientGrantType_integration_test.go ├── clientGrantType_test.go ├── client_integration_test.go ├── client_test.go ├── coverage.out ├── credentialsGrant.go ├── credentialsGrant_integration_test.go ├── credentialsGrant_test.go ├── implicitGrant.go ├── implicitGrantScope.go ├── implicitGrant_integration_test.go ├── implicitGrant_test.go ├── keys.go ├── keys_integration_test.go ├── keys_test.go ├── oauthDb.go ├── passwordGrant.go ├── passwordGrant_integration_test.go ├── passwordGrant_test.go ├── queries.go ├── redirectUri.go ├── redirectUri_integration_test.go ├── redirectUri_test.go ├── refreshToken.go ├── refreshToken_integration_test.go ├── refreshToken_test.go ├── roleUri.go ├── roleUri_integration_test.go ├── roleUri_test.go ├── role_integration_test.go ├── role_test.go ├── roles.go ├── runinttest.sh ├── scope.go ├── scope_integration_test.go └── scope_test.go ├── oauth2database ├── model.go └── oauth2Db.go ├── oauthclient ├── client.go ├── coverage.out ├── mockOauthClient.go ├── mockOauthClient_test.go ├── oauthClient.go └── oauthClient_test.go ├── rolecontrol ├── coverage.out ├── mockRoleControl.go ├── mockRoleControl_test.go ├── roleControl.go └── roleControl_test.go ├── runtest.sh ├── scripts ├── mysql │ └── go_auth2.sql ├── temp.sql ├── ulboraOauth2.mwb └── ulboraOauth2.mwb.bak ├── server.go ├── server_test.go1 ├── sonar-project.properties ├── start.sh ├── startMock.sh ├── static ├── authorizeApp.html ├── css │ ├── semantic.min.css │ ├── styles.css │ └── styles.less ├── head.html ├── images │ └── goAuth2.png ├── index.html ├── js │ ├── jquery-3.4.1.min.js │ └── semantic.min.js ├── login.html └── oauthError.html └── updateMods.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | /scripts/* linguist-documentation 2 | /static/* linguist-documentation -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Build 5 | 6 | on: 7 | push: 8 | branches: [ "master" ] 9 | pull_request: 10 | branches: [ "master" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | 19 | - name: Set up Go 20 | uses: actions/setup-go@v4 21 | with: 22 | go-version: 1.22.2 23 | 24 | - name: Install dependencies 25 | run: go get . 26 | 27 | - name: Build 28 | run: go build -v ./... 29 | 30 | - name: Test 31 | run: go test -v ./... 32 | 33 | # - name: Build the Docker image 34 | # run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) 35 | 36 | sonarcloud: 37 | name: SonarCloud 38 | runs-on: ubuntu-latest 39 | steps: 40 | - uses: actions/checkout@v4 41 | with: 42 | fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis 43 | - name: SonarCloud Scan 44 | uses: SonarSource/sonarcloud-github-action@master 45 | env: 46 | GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} # Needed to get PR information, if any 47 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /.github/workflows/docker.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | # GitHub recommends pinning actions to a commit SHA. 7 | # To get a newer version, you will need to update the SHA. 8 | # You can also reference a tag or branch, but the action may change without warning. 9 | 10 | name: Publish Docker image 11 | 12 | on: 13 | release: 14 | types: [published] 15 | 16 | jobs: 17 | push_to_registry: 18 | name: Push Docker image to Docker Hub 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Check out the repo 22 | uses: actions/checkout@v3 23 | 24 | - name: Log in to Docker Hub 25 | uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 26 | with: 27 | username: ${{ secrets.DOCKER_USERNAME }} 28 | password: ${{ secrets.DOCKER_PASSWORD }} 29 | 30 | - name: Extract metadata (tags, labels) for Docker 31 | id: meta 32 | uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 33 | with: 34 | images: ulboralabs/goauth2 35 | 36 | - name: Build and push Docker image 37 | uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc 38 | with: 39 | context: . 40 | push: true 41 | tags: ${{ steps.meta.outputs.tags }} 42 | labels: ${{ steps.meta.outputs.labels }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /nbproject/private/ -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | UlboraOauth2Server 4 | 5 | 6 | 7 | 8 | 9 | com.googlecode.goclipse.goBuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | 16 | com.googlecode.goclipse.core.goNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Launch", 9 | "type": "go", 10 | "request": "launch", 11 | "mode": "auto", 12 | "program": "${fileDirname}", 13 | "env": {}, 14 | "args": [] 15 | } 16 | ] 17 | } -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at kenulborapublic@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing guidelines 2 | 3 | ## Why 4 | GoAuth2 is written in Golang and if you would like to get experience with the language, this is a good place to start. 5 | If you have an interest in security, this is also a good place to start by helping to build a Golang Oauth2 server solution that is licensed under the GPLV3 license. 6 | 7 | ## Pull Request Checklist 8 | Before sending your pull requests, make sure you followed this list. 9 | 10 | - Read [contributing guidelines](CONTRIBUTING.md). 11 | - Read [Code of Conduct](CODE_OF_CONDUCT.md). 12 | 13 | 14 | ## Project organization 15 | 16 | * Branch `master` is always stable and release-ready. 17 | * Branch `develop` is for development and merged into `master` when stable. 18 | * Feature branches should be created for adding new features and merged into `develop` when ready. 19 | * Bug fix branches should be created for fixing bugs and merged into `develop` when ready. 20 | 21 | ## Submitting a pull request 22 | 23 | 1. Find an issue to work on, or create a new one. *Avoid duplicates, please check existing issues!* 24 | 2. Fork the repo, or make sure you are synced with the latest changes on `develop`. 25 | 3. Create a new branch with a sweet name: `git checkout -b issue_<##>_`. 26 | 4. Do some programming. 27 | 5. Write unit tests. 28 | 6. Keep your code nice and clean by adhering to the coding standards & guidelines below. 29 | 7. Don't break unit tests or functionality. 30 | 8. Update the documentation header comments if needed. 31 | 9. **Rebase on `develop` branch and resolve any conflicts _before submitting a pull request!_** 32 | 10. Submit a pull request to the `develop` branch. 33 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:22.04 2 | 3 | #RUN sudo apt-get update 4 | RUN apt-get update 5 | RUN apt-get install -y ca-certificates 6 | ADD main /main 7 | ADD start.sh /start.sh 8 | ADD entrypoint.sh /entrypoint.sh 9 | ADD static /static 10 | WORKDIR / 11 | 12 | EXPOSE 3000 13 | ENTRYPOINT ["/entrypoint.sh"] 14 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](./static/images/goAuth2.png) 2 | 3 | A complete standalone Oauth2 Server RFC 6749 implementation written in Golang and licensed under the GPL V3 license. 4 | 5 | 6 | ### [Installation](https://github.com/Ulbora/GoAuth2/wiki/Installation) 7 | 8 | 9 | ### [Documentation](https://github.com/Ulbora/GoAuth2/wiki) 10 | 11 | --- 12 | 13 | [![GitHub Actions Build](https://github.com/Ulbora/GoAuth2/actions/workflows/build.yml/badge.svg)](https://github.com/Ulbora/GoAuth2/actions/workflows/build.yml) 14 | [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Ulbora_GoAuth2&metric=alert_status)](https://sonarcloud.io/dashboard?id=Ulbora_GoAuth2) 15 | [![Go Report Card](https://goreportcard.com/badge/github.com/Ulbora/GoAuth2)](https://goreportcard.com/report/github.com/Ulbora/GoAuth2) 16 | 17 | 18 | 19 | GoAuth2 is an Oauth2 server implementation written in Golang and the security layer for Six910 open source eCommerce solution. Currently authorization code, 20 | implicit, and client credentials grant, and password grant types are supported. 21 | 22 | GoAuth2 issues a compressed enhanced JWT token that can be used to secure individual REST endpoints for users using roles. REST endpoints can be coded to validate the JWT token using the user's role. There is also a access token REST service that can validate a compressed token. Token compression can be turned off at startup if desired. 23 | 24 | --- 25 | 26 | ### GoAuth2 will provide the security infrastructure for Six910 the new open source eCommerce Platform solution from Ulbora Labs LLC, Six910. 27 | #### (Six910 is an eCommerce Platform server solution written in golang that provides REST endpoints for backend operations. Six910 frontend soltutions is also written in golang.) 28 | 29 | --- 30 | 31 | This server should run behind nginx and nginx should handle certs. 32 | 33 | If you would **like to contribute** to this project, **send a pull request**. 34 | 35 | 36 | Contributors: 37 | Ken Williamson 38 | 39 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | # GO111MODULE=on go build -o main *.go 2 | # GO111MODULE=on go build 3 | GOOS=linux GOARCH=amd64 go build -o main *.go -------------------------------------------------------------------------------- /compresstoken/compressToken.go: -------------------------------------------------------------------------------- 1 | //Package compresstoken ... 2 | package compresstoken 3 | 4 | import ( 5 | "bytes" 6 | "compress/flate" 7 | "compress/zlib" 8 | "encoding/base64" 9 | "io" 10 | ) 11 | 12 | /* 13 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 14 | All rights reserved. 15 | 16 | Copyright (C) 2019 Ken Williamson 17 | All rights reserved. 18 | 19 | This program is free software: you can redistribute it and/or modify 20 | it under the terms of the GNU General Public License as published by 21 | the Free Software Foundation, either version 3 of the License, or 22 | (at your option) any later version. 23 | This program is distributed in the hope that it will be useful, 24 | but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | GNU General Public License for more details. 27 | You should have received a copy of the GNU General Public License 28 | along with this program. If not, see . 29 | 30 | */ 31 | 32 | //JwtCompress JwtCompress 33 | type JwtCompress struct { 34 | } 35 | 36 | //CompressJwt CompressJwt 37 | func (c *JwtCompress) CompressJwt(jwt string) string { 38 | //compress jwt with zlib and package with base64 39 | var rtn string 40 | var b bytes.Buffer 41 | w, err := zlib.NewWriterLevel(&b, flate.BestCompression) 42 | if err == nil { 43 | w.Write([]byte(jwt)) 44 | w.Close() 45 | rtn = base64.StdEncoding.EncodeToString(b.Bytes()) 46 | } 47 | return rtn 48 | } 49 | 50 | //UnCompressJwt UnCompressJwt 51 | func (c *JwtCompress) UnCompressJwt(cjwt string) string { 52 | //uncompress jwt with zlib after converting from base64 53 | var rtn string 54 | var b bytes.Buffer 55 | decoded, derr := base64.StdEncoding.DecodeString(cjwt) 56 | if derr == nil { 57 | b.Write(decoded) 58 | r, err := zlib.NewReader(&b) 59 | if err == nil { 60 | var out bytes.Buffer 61 | io.Copy(&out, r) 62 | r.Close() 63 | rtn = out.String() 64 | } 65 | } 66 | return rtn 67 | } 68 | -------------------------------------------------------------------------------- /compresstoken/coverage.out: -------------------------------------------------------------------------------- 1 | mode: set 2 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:37.54,42.16 4 1 3 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:42.16,46.3 3 1 4 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:47.2,47.12 1 1 5 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:51.57,56.17 4 1 6 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:56.17,59.17 3 1 7 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:59.17,64.4 4 1 8 | github.com/Ulbora/GoAuth2/compresstoken/compressToken.go:66.2,66.12 1 1 9 | -------------------------------------------------------------------------------- /coverage.out: -------------------------------------------------------------------------------- 1 | mode: set 2 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./start.sh 4 | 5 | 6 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Ulbora/GoAuth2 2 | 3 | go 1.22 4 | 5 | require ( 6 | github.com/Ulbora/GoProxy v1.0.1 7 | github.com/Ulbora/Level_Logger v1.0.2 8 | github.com/Ulbora/auth_interface v1.0.0 9 | github.com/Ulbora/dbinterface v1.0.5 10 | github.com/Ulbora/dbinterface_mysql v1.0.7 11 | github.com/Ulbora/default_auth v1.0.0 12 | github.com/Ulbora/go-sessions v1.0.2 13 | github.com/gbrlsnchs/jwt/v3 v3.0.0-rc.1 14 | github.com/gorilla/mux v1.7.3 15 | github.com/gorilla/sessions v1.2.0 16 | ) 17 | 18 | require ( 19 | github.com/go-sql-driver/mysql v1.5.0 // indirect 20 | github.com/google/go-cmp v0.4.0 // indirect 21 | github.com/gorilla/securecookie v1.1.1 // indirect 22 | github.com/magefile/mage v1.9.0 // indirect 23 | golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d // indirect 24 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect 25 | ) 26 | -------------------------------------------------------------------------------- /handlers/loggingHandler.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "encoding/json" 6 | "fmt" 7 | "net/http" 8 | "os" 9 | "strings" 10 | 11 | lg "github.com/Ulbora/Level_Logger" 12 | ) 13 | 14 | /* 15 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 16 | All rights reserved. 17 | 18 | Copyright (C) 2019 Ken Williamson 19 | All rights reserved. 20 | 21 | This program is free software: you can redistribute it and/or modify 22 | it under the terms of the GNU General Public License as published by 23 | the Free Software Foundation, either version 3 of the License, or 24 | (at your option) any later version. 25 | This program is distributed in the hope that it will be useful, 26 | but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | GNU General Public License for more details. 29 | You should have received a copy of the GNU General Public License 30 | along with this program. If not, see . 31 | 32 | */ 33 | 34 | //LogLevel LogLevel 35 | type LogLevel struct { 36 | Level string `json:"logLevel"` 37 | } 38 | 39 | //LogResponse LogResponse 40 | type LogResponse struct { 41 | Success bool `json:"success"` 42 | LogLevel string `json:"logLevel"` 43 | } 44 | 45 | const ( 46 | defaultLoggingKey = "45sdbb2345" 47 | 48 | debugLevel = "DEBUG" 49 | infoLevel = "INFO" 50 | allLevel = "ALL" 51 | offLevel = "OFF" 52 | ) 53 | 54 | //SetLogLevel SetLogLevel 55 | func (h *OauthRestHandler) SetLogLevel(w http.ResponseWriter, r *http.Request) { 56 | var logRes LogResponse 57 | h.SetContentType(w) 58 | logContOk := h.CheckContent(r) 59 | 60 | //fmt.Println("conOk: ", logContOk) 61 | 62 | if !logContOk { 63 | http.Error(w, "json required", http.StatusUnsupportedMediaType) 64 | } else { 65 | var loggingKey string 66 | if os.Getenv("LOGGING_KEY") != "" { 67 | loggingKey = os.Getenv("LOGGING_KEY") 68 | } else { 69 | loggingKey = defaultLoggingKey 70 | } 71 | loggingKeyHdr := r.Header.Get("Logging_KEY") 72 | if loggingKey == loggingKeyHdr { 73 | var lv LogLevel 74 | lgsuc, lgerr := h.ProcessBody(r, &lv) 75 | //fmt.Println("lgsuc: ", lgsuc) 76 | //fmt.Println("LogLevel: ", lv) 77 | //fmt.Println("lgerr: ", lgerr) 78 | if !lgsuc && lgerr != nil { 79 | http.Error(w, lgerr.Error(), http.StatusBadRequest) 80 | } else { 81 | switch strings.ToUpper(lv.Level) { 82 | case debugLevel: 83 | h.Log.LogLevel = lg.DebugLevel 84 | logRes.Success = true 85 | logRes.LogLevel = debugLevel 86 | case infoLevel: 87 | h.Log.LogLevel = lg.InfoLevel 88 | logRes.Success = true 89 | logRes.LogLevel = infoLevel 90 | case allLevel: 91 | h.Log.LogLevel = lg.AllLevel 92 | logRes.Success = true 93 | logRes.LogLevel = allLevel 94 | case offLevel: 95 | h.Log.LogLevel = lg.OffLevel 96 | logRes.Success = true 97 | logRes.LogLevel = offLevel 98 | } 99 | } 100 | } else { 101 | w.WriteHeader(http.StatusUnauthorized) 102 | } 103 | resJSON, _ := json.Marshal(logRes) 104 | fmt.Fprint(w, string(resJSON)) 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /handlers/oauthHandlers.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "encoding/json" 6 | "errors" 7 | 8 | "net/http" 9 | 10 | cp "github.com/Ulbora/GoAuth2/compresstoken" 11 | m "github.com/Ulbora/GoAuth2/managers" 12 | oa "github.com/Ulbora/GoAuth2/oauthclient" 13 | rc "github.com/Ulbora/GoAuth2/rolecontrol" 14 | lg "github.com/Ulbora/Level_Logger" 15 | ) 16 | 17 | /* 18 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 19 | All rights reserved. 20 | 21 | Copyright (C) 2019 Ken Williamson 22 | All rights reserved. 23 | 24 | This program is free software: you can redistribute it and/or modify 25 | it under the terms of the GNU General Public License as published by 26 | the Free Software Foundation, either version 3 of the License, or 27 | (at your option) any later version. 28 | This program is distributed in the hope that it will be useful, 29 | but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | GNU General Public License for more details. 32 | You should have received a copy of the GNU General Public License 33 | along with this program. If not, see . 34 | 35 | */ 36 | 37 | //OauthRestHandler OauthRestHandler 38 | type OauthRestHandler struct { 39 | Manager m.Manager 40 | Client oa.Client 41 | AssetControl rc.AssetControl 42 | TokenCompressed bool 43 | JwtCompress cp.JwtCompress 44 | Log *lg.Logger 45 | } 46 | 47 | //GetNewRestHandler GetNewRestHandler 48 | func (h *OauthRestHandler) GetNewRestHandler() RestHandler { 49 | var rh RestHandler 50 | rh = h 51 | return rh 52 | } 53 | 54 | //CheckContent CheckContent 55 | func (h *OauthRestHandler) CheckContent(r *http.Request) bool { 56 | var rtn bool 57 | cType := r.Header.Get("Content-Type") 58 | if cType == "application/json" { 59 | // http.Error(w, "json required", http.StatusUnsupportedMediaType) 60 | rtn = true 61 | } 62 | return rtn 63 | } 64 | 65 | //SetContentType SetContentType 66 | func (h *OauthRestHandler) SetContentType(w http.ResponseWriter) { 67 | w.Header().Set("Content-Type", "application/json") 68 | } 69 | 70 | //ProcessBody ProcessBody 71 | func (h *OauthRestHandler) ProcessBody(r *http.Request, obj interface{}) (bool, error) { 72 | var suc bool 73 | var err error 74 | //fmt.Println("r.Body: ", r.Body) 75 | if r.Body != nil { 76 | decoder := json.NewDecoder(r.Body) 77 | //fmt.Println("decoder: ", decoder) 78 | err = decoder.Decode(obj) 79 | //fmt.Println("decoder: ", decoder) 80 | if err != nil { 81 | //log.Println("Decode Error: ", err.Error()) 82 | h.Log.Error("Decode Error: ", err.Error()) 83 | } else { 84 | suc = true 85 | } 86 | } else { 87 | err = errors.New("Bad Body") 88 | } 89 | 90 | return suc, err 91 | } 92 | -------------------------------------------------------------------------------- /handlers/oauthHandlers_test.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "bytes" 6 | "encoding/json" 7 | "net/http" 8 | "testing" 9 | 10 | lg "github.com/Ulbora/Level_Logger" 11 | // m "github.com/Ulbora/GoAuth2/managers" 12 | // oa "github.com/Ulbora/GoAuth2/oauthclient" 13 | // rc "github.com/Ulbora/GoAuth2/rolecontrol" 14 | ) 15 | 16 | type testObj struct { 17 | Valid bool `json:"valid"` 18 | Code string `json:"code"` 19 | } 20 | 21 | func TestOauthRestHandler_ProcessBody(t *testing.T) { 22 | var oh OauthRestHandler 23 | var l lg.Logger 24 | oh.Log = &l 25 | var robj testObj 26 | robj.Valid = true 27 | robj.Code = "3" 28 | // var res http.Response 29 | // res.Body = ioutil.NopCloser(bytes.NewBufferString(`{"valid":true, "code":"1"}`)) 30 | var sURL = "http://localhost/test" 31 | aJSON, _ := json.Marshal(robj) 32 | r, _ := http.NewRequest("POST", sURL, bytes.NewBuffer(aJSON)) 33 | var obj testObj 34 | suc, _ := oh.ProcessBody(r, &obj) 35 | if !suc || obj.Valid != true || obj.Code != "3" { 36 | t.Fail() 37 | } 38 | } 39 | 40 | func TestOauthRestHandler_ProcessBodyBadObj(t *testing.T) { 41 | var oh OauthRestHandler 42 | var l lg.Logger 43 | oh.Log = &l 44 | var robj testObj 45 | robj.Valid = true 46 | robj.Code = "3" 47 | // var res http.Response 48 | // res.Body = ioutil.NopCloser(bytes.NewBufferString(`{"valid":true, "code":"1"}`)) 49 | var sURL = "http://localhost/test" 50 | aJSON, _ := json.Marshal(robj) 51 | r, _ := http.NewRequest("POST", sURL, bytes.NewBuffer(aJSON)) 52 | var obj testObj 53 | suc, _ := oh.ProcessBody(r, nil) 54 | if suc || obj.Valid != false || obj.Code != "" { 55 | t.Fail() 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /handlers/testHtmls/test.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ulbora/GoAuth2/5f98770fcff7df6f14cc176de2f11bf0ac60979b/handlers/testHtmls/test.html -------------------------------------------------------------------------------- /handlers/utilites.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "fmt" 6 | 7 | b64 "encoding/base64" 8 | "encoding/json" 9 | 10 | m "github.com/Ulbora/GoAuth2/managers" 11 | msdb "github.com/Ulbora/GoAuth2/mysqldb" 12 | odb "github.com/Ulbora/GoAuth2/oauth2database" 13 | oa "github.com/Ulbora/GoAuth2/oauthclient" 14 | rc "github.com/Ulbora/GoAuth2/rolecontrol" 15 | px "github.com/Ulbora/GoProxy" 16 | lg "github.com/Ulbora/Level_Logger" 17 | db "github.com/Ulbora/dbinterface" 18 | dau "github.com/Ulbora/default_auth" 19 | // mdb "github.com/Ulbora/dbinterface_mysql" 20 | ) 21 | 22 | /* 23 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 24 | All rights reserved. 25 | 26 | Copyright (C) 2019 Ken Williamson 27 | All rights reserved. 28 | 29 | This program is free software: you can redistribute it and/or modify 30 | it under the terms of the GNU General Public License as published by 31 | the Free Software Foundation, either version 3 of the License, or 32 | (at your option) any later version. 33 | This program is distributed in the hope that it will be useful, 34 | but WITHOUT ANY WARRANTY; without even the implied warranty of 35 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 36 | GNU General Public License for more details. 37 | You should have received a copy of the GNU General Public License 38 | along with this program. If not, see . 39 | 40 | */ 41 | 42 | //UseWebHandler UseWebHandler 43 | func UseWebHandler(dbi db.Database, compressJtw bool, authURL string, logger *lg.Logger, tokenParams *m.TokenParams) *OauthWebHandler { 44 | var oauthManagerw m.OauthManager 45 | oauthManagerw.TokenParams = tokenParams 46 | oauthManagerw.Log = logger 47 | var oauthMySqldbw msdb.MySQLOauthDB 48 | oauthMySqldbw.Log = logger 49 | oauthMySqldbw.DB = dbi 50 | var oauthDbw odb.Oauth2DB 51 | oauthDbw = &oauthMySqldbw 52 | oauthManagerw.Db = oauthDbw 53 | var userServiceProxy px.GoProxy 54 | oauthManagerw.Proxy = userServiceProxy.GetNewProxy() 55 | 56 | var proxy px.GoProxy 57 | var da dau.DefaultAuth 58 | da.AuthServerURL = authURL 59 | da.Proxy = proxy.GetNewProxy() 60 | oauthManagerw.AuthService = da.GetNew() 61 | 62 | var wh OauthWebHandler 63 | wh.Log = logger 64 | wh.Manager = &oauthManagerw 65 | wh.TokenCompressed = compressJtw 66 | return &wh 67 | } 68 | 69 | //UseRestHandler UseRestHandler 70 | func UseRestHandler(dbi db.Database, assets string, compressJtw bool, authURL string, logger *lg.Logger, tokenParams *m.TokenParams) *OauthRestHandler { 71 | var oauthManager m.OauthManager 72 | oauthManager.TokenParams = tokenParams 73 | oauthManager.Log = logger 74 | var oauthMySqldb msdb.MySQLOauthDB 75 | oauthMySqldb.Log = logger 76 | oauthMySqldb.DB = dbi 77 | var oauthDb odb.Oauth2DB 78 | oauthDb = &oauthMySqldb 79 | oauthManager.Db = oauthDb 80 | 81 | var proxy px.GoProxy 82 | var da dau.DefaultAuth 83 | da.AuthServerURL = authURL 84 | da.Proxy = proxy.GetNewProxy() 85 | oauthManager.AuthService = da.GetNew() 86 | 87 | var rh OauthRestHandler 88 | rh.Log = logger 89 | 90 | rh.Manager = &oauthManager 91 | 92 | var clt oa.OauthClient 93 | clt.Manager = &oauthManager 94 | clt.TokenCompressed = compressJtw 95 | clt.Log = logger 96 | rh.Client = &clt 97 | 98 | var curs []rc.ControlledURL 99 | if assets != "" { 100 | //parse assets 101 | sDec, err := b64.StdEncoding.DecodeString(assets) 102 | if err == nil { 103 | json.Unmarshal(sDec, &curs) 104 | fmt.Println("curs: ", curs) 105 | } 106 | } 107 | var ac rc.OauthAssets 108 | ac.AddControledURLs(&curs) 109 | rh.AssetControl = &ac 110 | rh.TokenCompressed = compressJtw 111 | return &rh 112 | } 113 | -------------------------------------------------------------------------------- /handlers/utilitesMock_test.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "testing" 6 | ) 7 | 8 | func TestUseMockWeb(t *testing.T) { 9 | h := UseMockWeb() 10 | if h == nil { 11 | t.Fail() 12 | } 13 | } 14 | 15 | func TestUseMockRest(t *testing.T) { 16 | h := UseMockRest() 17 | if h == nil { 18 | t.Fail() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /handlers/utilites_test.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "testing" 6 | 7 | db "github.com/Ulbora/dbinterface" 8 | ) 9 | 10 | func TestUseHanders_UseWebHandler(t *testing.T) { 11 | var dbi db.Database 12 | h := UseWebHandler(dbi, false, "url", nil, nil) 13 | if h == nil { 14 | t.Fail() 15 | } 16 | } 17 | 18 | func TestUseHanders_UseRestHandler(t *testing.T) { 19 | var dbi db.Database 20 | var assets = "WwogICB7CiAgICAgICJ1cmwiOiIvdWxib3JhL3JzL2NsaWVudEFsbG93ZWRVcmkvYWRkIiwKICAgICAgImFzc2V0cyI6WwogICAgICAgICB7CiAgICAgICAgICAgICJjb250cm9sbGVkQXNzZXQiOiJ1bGJvcmEiLAogICAgICAgICAgICAiYWxsb3dlZFJvbGUiOiJzdXBlckFkbWluIgogICAgICAgICB9CiAgICAgIF0KICAgfSwKICAgewogICAgICAidXJsIjoiL3VsYm9yYS9ycy9jbGllbnRBbGxvd2VkVXJpL3VwZGF0ZSIsCiAgICAgICJhc3NldHMiOlsKICAgICAgICAgewogICAgICAgICAgICAiY29udHJvbGxlZEFzc2V0IjoidWxib3JhIiwKICAgICAgICAgICAgImFsbG93ZWRSb2xlIjoic3VwZXJBZG1pbiIKICAgICAgICAgfQogICAgICBdCiAgIH0sCiAgIHsKICAgICAgInVybCI6Ii91bGJvcmEvcnMvY2xpZW50Um9sZS9hZGQiLAogICAgICAiYXNzZXRzIjpbCiAgICAgICAgIHsKICAgICAgICAgICAgImNvbnRyb2xsZWRBc3NldCI6InN1cGVyQWRtaW4iLAogICAgICAgICAgICAiYWxsb3dlZFJvbGUiOiJzdXBlckFkbWluIgogICAgICAgICB9CiAgICAgIF0KICAgfQpd" 21 | h := UseRestHandler(dbi, assets, false, "url", nil, nil) 22 | if h == nil { 23 | t.Fail() 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /handlers/validateTokenRestHandlers.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "encoding/json" 6 | "fmt" 7 | "net/http" 8 | 9 | m "github.com/Ulbora/GoAuth2/managers" 10 | ) 11 | 12 | /* 13 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 14 | All rights reserved. 15 | 16 | Copyright (C) 2019 Ken Williamson 17 | All rights reserved. 18 | 19 | This program is free software: you can redistribute it and/or modify 20 | it under the terms of the GNU General Public License as published by 21 | the Free Software Foundation, either version 3 of the License, or 22 | (at your option) any later version. 23 | This program is distributed in the hope that it will be useful, 24 | but WITHOUT ANY WARRANTY; without even the implied warranty of 25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 | GNU General Public License for more details. 27 | You should have received a copy of the GNU General Public License 28 | along with this program. If not, see . 29 | 30 | */ 31 | 32 | //ValidationResponse ValidationResponse 33 | type ValidationResponse struct { 34 | Valid bool `json:"valid"` 35 | } 36 | 37 | //ValidateAccessToken ValidateAccessToken 38 | func (h *OauthRestHandler) ValidateAccessToken(w http.ResponseWriter, r *http.Request) { 39 | var vdtr m.ValidateAccessTokenReq 40 | vdtsuc, gerr := h.ProcessBody(r, &vdtr) 41 | if h.TokenCompressed { 42 | vdtr.AccessToken = h.JwtCompress.UnCompressJwt(vdtr.AccessToken) 43 | fmt.Println("uncompressed token in validate: ", vdtr.AccessToken) 44 | } 45 | h.Log.Debug("vdtsuc: ", vdtsuc) 46 | h.Log.Debug("vdtr: ", vdtr) 47 | h.Log.Debug("gerr: ", gerr) 48 | if vdtsuc && gerr == nil { 49 | h.SetContentType(w) 50 | vdtContOk := h.CheckContent(r) 51 | h.Log.Debug("conOk: ", vdtContOk) 52 | if !vdtContOk { 53 | http.Error(w, "json required", http.StatusUnsupportedMediaType) 54 | } else { 55 | vdtSuc := h.Manager.ValidateAccessToken(&vdtr) 56 | h.Log.Debug("vdtSuc: ", vdtSuc) 57 | var rtn ValidationResponse 58 | if vdtSuc { 59 | rtn.Valid = vdtSuc 60 | } 61 | w.WriteHeader(http.StatusOK) 62 | resJSON, _ := json.Marshal(rtn) 63 | fmt.Fprint(w, string(resJSON)) 64 | } 65 | } else { 66 | http.Error(w, gerr.Error(), http.StatusBadRequest) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /handlers/webIndexHandlers.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "fmt" 6 | "net/http" 7 | ) 8 | 9 | /* 10 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 11 | All rights reserved. 12 | 13 | Copyright (C) 2019 Ken Williamson 14 | All rights reserved. 15 | 16 | This program is free software: you can redistribute it and/or modify 17 | it under the terms of the GNU General Public License as published by 18 | the Free Software Foundation, either version 3 of the License, or 19 | (at your option) any later version. 20 | This program is distributed in the hope that it will be useful, 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | GNU General Public License for more details. 24 | You should have received a copy of the GNU General Public License 25 | along with this program. If not, see . 26 | 27 | */ 28 | 29 | //Index Index 30 | func (h *OauthWebHandler) Index(w http.ResponseWriter, r *http.Request) { 31 | fmt.Println("index test") 32 | //var lpg PageParams 33 | //lpg.Title = loginPageTitle 34 | h.Log.Debug("template: ", h.Templates) 35 | h.Templates.ExecuteTemplate(w, indexHTML, nil) 36 | } 37 | -------------------------------------------------------------------------------- /handlers/webIndexHandlers_test.go: -------------------------------------------------------------------------------- 1 | //Package handlers ... 2 | package handlers 3 | 4 | import ( 5 | "fmt" 6 | "html/template" 7 | "net/http" 8 | "net/http/httptest" 9 | "testing" 10 | 11 | m "github.com/Ulbora/GoAuth2/managers" 12 | lg "github.com/Ulbora/Level_Logger" 13 | ) 14 | 15 | func TestOauthWebHandlerIndex_Index(t *testing.T) { 16 | var om m.MockManager 17 | 18 | om.MockAuthCodeAuthorized = true 19 | om.MockAuthCodeAuthorizeSuccess = true 20 | om.MockAuthCode = 55 21 | om.MockAuthCodeString = "rr666" 22 | 23 | var wh OauthWebHandler 24 | var l lg.Logger 25 | wh.Log = &l 26 | wh.Templates = template.Must(template.ParseFiles("testHtmls/test.html")) 27 | wh.Manager = &om 28 | h := wh.GetNewWebHandler() 29 | r, _ := http.NewRequest("GET", "/test", nil) 30 | //r.Header.Set("Content-Type", "application/json") 31 | w := httptest.NewRecorder() 32 | 33 | h.Index(w, r) 34 | fmt.Println("code: ", w.Code) 35 | 36 | if w.Code != 200 { 37 | t.Fail() 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ulbora/GoAuth2/5f98770fcff7df6f14cc176de2f11bf0ac60979b/main -------------------------------------------------------------------------------- /managers/accessTokenManager.go: -------------------------------------------------------------------------------- 1 | //Package managers ... 2 | package managers 3 | 4 | /* 5 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 6 | All rights reserved. 7 | 8 | Copyright (C) 2019 Ken Williamson 9 | All rights reserved. 10 | 11 | This program is free software: you can redistribute it and/or modify 12 | it under the terms of the GNU General Public License as published by 13 | the Free Software Foundation, either version 3 of the License, or 14 | (at your option) any later version. 15 | This program is distributed in the hope that it will be useful, 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | GNU General Public License for more details. 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | 22 | */ 23 | 24 | //GenerateAccessToken GenerateAccessToken 25 | func (m *OauthManager) GenerateAccessToken(pl *Payload) string { 26 | var token string 27 | m.Log.Debug("m.TokenParams: ", m.TokenParams) 28 | var theKey string 29 | var theIssuer string 30 | var theAudience string 31 | if m.TokenParams != nil && m.TokenParams.AccessTokenKey != "" { 32 | theKey = m.TokenParams.AccessTokenKey 33 | } else { 34 | theKey = m.Db.GetAccessTokenKey() 35 | } 36 | if m.TokenParams != nil && m.TokenParams.Issuer != "" { 37 | theIssuer = m.TokenParams.Issuer 38 | } else { 39 | theIssuer = tokenIssuer 40 | } 41 | if m.TokenParams != nil && m.TokenParams.Audience != "" { 42 | theAudience = m.TokenParams.Audience 43 | } else { 44 | theAudience = tokenAudience 45 | } 46 | //key := m.Db.GetAccessTokenKey() 47 | //if key != "" { 48 | pl.SecretKey = theKey 49 | pl.Subject = pl.Grant 50 | pl.Issuer = theIssuer 51 | pl.Audience = theAudience 52 | token = m.GenerateJwtToken(pl) 53 | //} 54 | return token 55 | } 56 | -------------------------------------------------------------------------------- /managers/clientAllowedUriManager.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | //"fmt" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //ClientAllowedURI ClientAllowedURI 30 | type ClientAllowedURI struct { 31 | ID int64 `json:"id"` 32 | URI string `json:"uri"` 33 | ClientID int64 `json:"clientId"` 34 | } 35 | 36 | //AddClientAllowedURI AddClientAllowedURI 37 | func (m *OauthManager) AddClientAllowedURI(au *ClientAllowedURI) (bool, int64) { 38 | var cu odb.ClientAllowedURI 39 | cu.URI = au.URI 40 | cu.ClientID = au.ClientID 41 | suc, id := m.Db.AddClientAllowedURI(&cu) 42 | return suc, id 43 | } 44 | 45 | //UpdateClientAllowedURI UpdateClientAllowedURI 46 | func (m *OauthManager) UpdateClientAllowedURI(au *ClientAllowedURI) bool { 47 | var cu odb.ClientAllowedURI 48 | cu.URI = au.URI 49 | cu.ID = au.ID 50 | suc := m.Db.UpdateClientAllowedURI(&cu) 51 | return suc 52 | } 53 | 54 | //GetClientAllowedURI GetClientAllowedURI 55 | func (m *OauthManager) GetClientAllowedURI(id int64) *ClientAllowedURI { 56 | var rtn ClientAllowedURI 57 | au := m.Db.GetClientAllowedURIByID(id) 58 | rtn.ID = au.ID 59 | rtn.URI = au.URI 60 | rtn.ClientID = au.ClientID 61 | return &rtn 62 | } 63 | 64 | //GetClientAllowedURIList GetClientAllowedURIList 65 | func (m *OauthManager) GetClientAllowedURIList(clientID int64) *[]ClientAllowedURI { 66 | var rtn = []ClientAllowedURI{} 67 | aul := m.Db.GetClientAllowedURIList(clientID) 68 | for _, au := range *aul { 69 | var u ClientAllowedURI 70 | u.ID = au.ID 71 | u.URI = au.URI 72 | u.ClientID = au.ClientID 73 | rtn = append(rtn, u) 74 | } 75 | return &rtn 76 | } 77 | 78 | //DeleteClientAllowedURI DeleteClientAllowedURI 79 | func (m *OauthManager) DeleteClientAllowedURI(id int64) bool { 80 | suc := m.Db.DeleteClientAllowedURI(id) 81 | return suc 82 | } 83 | -------------------------------------------------------------------------------- /managers/clientRedirectUriManager.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | //"fmt" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //ClientRedirectURI ClientRedirectURI 30 | type ClientRedirectURI struct { 31 | ID int64 `json:"id"` 32 | URI string `json:"uri"` 33 | ClientID int64 `json:"clientId"` 34 | } 35 | 36 | //AddClientRedirectURI AddClientRedirectURI 37 | func (m *OauthManager) AddClientRedirectURI(ru *ClientRedirectURI) (bool, int64) { 38 | // var suc bool 39 | // var id int64 40 | var cru odb.ClientRedirectURI 41 | cru.URI = ru.URI 42 | cru.ClientID = ru.ClientID 43 | suc, id := m.Db.AddClientRedirectURI(nil, &cru) 44 | return suc, id 45 | } 46 | 47 | //GetClientRedirectURIList GetClientRedirectURIList 48 | func (m *OauthManager) GetClientRedirectURIList(clientID int64) *[]ClientRedirectURI { 49 | var rtn = []ClientRedirectURI{} 50 | ul := m.Db.GetClientRedirectURIList(clientID) 51 | for _, u := range *ul { 52 | var ui ClientRedirectURI 53 | ui.ID = u.ID 54 | ui.URI = u.URI 55 | ui.ClientID = u.ClientID 56 | rtn = append(rtn, ui) 57 | } 58 | return &rtn 59 | } 60 | 61 | //DeleteClientRedirectURI DeleteClientRedirectURI 62 | func (m *OauthManager) DeleteClientRedirectURI(id int64) bool { 63 | suc := m.Db.DeleteClientRedirectURI(nil, id) 64 | return suc 65 | } 66 | -------------------------------------------------------------------------------- /managers/clientRedirectUriManager_test.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "testing" 5 | 6 | lg "github.com/Ulbora/Level_Logger" 7 | db "github.com/Ulbora/dbinterface" 8 | mdb "github.com/Ulbora/dbinterface_mysql" 9 | 10 | msdb "github.com/Ulbora/GoAuth2/mysqldb" 11 | odb "github.com/Ulbora/GoAuth2/oauth2database" 12 | ) 13 | 14 | func TestOauthManagerURI_AddClientRedirectURI(t *testing.T) { 15 | 16 | var dbAu db.Database 17 | var odbAu odb.Oauth2DB 18 | var mydb mdb.MyDBMock 19 | mydb.Host = "localhost:3306" 20 | mydb.User = "admin" 21 | mydb.Password = "admin" 22 | mydb.Database = "ulbora_oauth2_server" 23 | dbAu = &mydb 24 | 25 | var mTestRow db.DbRow 26 | mTestRow.Row = []string{} 27 | mydb.MockTestRow = &mTestRow 28 | 29 | mydb.MockInsertID1 = 2 30 | mydb.MockInsertSuccess1 = true 31 | 32 | // var rows [][]string 33 | // row1 := []string{"1", "code", "2"} 34 | // rows = append(rows, row1) 35 | // var dbrows db.DbRows 36 | // dbrows.Rows = rows 37 | // mydb.MockRows1 = &dbrows 38 | 39 | var moadb msdb.MySQLOauthDB 40 | moadb.DB = dbAu 41 | 42 | odbAu = &moadb 43 | 44 | var man OauthManager 45 | var l lg.Logger 46 | man.Log = &l 47 | moadb.Log = &l 48 | man.Db = odbAu 49 | var m Manager 50 | m = &man 51 | 52 | var uri ClientRedirectURI 53 | uri.URI = "test" 54 | suc, id := m.AddClientRedirectURI(&uri) 55 | if !suc || id != 2 { 56 | t.Fail() 57 | } 58 | 59 | } 60 | 61 | func TestOauthManagerURI_GetClientRedirectURI(t *testing.T) { 62 | 63 | var dbAu db.Database 64 | var odbAu odb.Oauth2DB 65 | var mydb mdb.MyDBMock 66 | mydb.Host = "localhost:3306" 67 | mydb.User = "admin" 68 | mydb.Password = "admin" 69 | mydb.Database = "ulbora_oauth2_server" 70 | dbAu = &mydb 71 | 72 | var mTestRow db.DbRow 73 | mTestRow.Row = []string{} 74 | mydb.MockTestRow = &mTestRow 75 | 76 | // mydb.MockInsertID1 = 2 77 | // mydb.MockInsertSuccess1 = true 78 | 79 | var rows [][]string 80 | row1 := []string{"1", "testUrl", "2"} 81 | rows = append(rows, row1) 82 | row2 := []string{"2", "testUrl", "3"} 83 | rows = append(rows, row2) 84 | var dbrows db.DbRows 85 | dbrows.Rows = rows 86 | mydb.MockRows1 = &dbrows 87 | 88 | var moadb msdb.MySQLOauthDB 89 | moadb.DB = dbAu 90 | 91 | odbAu = &moadb 92 | 93 | var man OauthManager 94 | var l lg.Logger 95 | man.Log = &l 96 | moadb.Log = &l 97 | man.Db = odbAu 98 | var m Manager 99 | m = &man 100 | 101 | ul := m.GetClientRedirectURIList(2) 102 | if len(*ul) > 2 || (*ul)[1].ID != 2 { 103 | t.Fail() 104 | } 105 | 106 | } 107 | 108 | func TestOauthManagerURI_DeleteClientRedirectURI(t *testing.T) { 109 | 110 | var dbAu db.Database 111 | var odbAu odb.Oauth2DB 112 | var mydb mdb.MyDBMock 113 | mydb.Host = "localhost:3306" 114 | mydb.User = "admin" 115 | mydb.Password = "admin" 116 | mydb.Database = "ulbora_oauth2_server" 117 | dbAu = &mydb 118 | 119 | var mTestRow db.DbRow 120 | mTestRow.Row = []string{} 121 | mydb.MockTestRow = &mTestRow 122 | 123 | //mydb.MockInsertID1 = 2 124 | mydb.MockDeleteSuccess1 = true 125 | 126 | // var rows [][]string 127 | // row1 := []string{"1", "code", "2"} 128 | // rows = append(rows, row1) 129 | // var dbrows db.DbRows 130 | // dbrows.Rows = rows 131 | // mydb.MockRows1 = &dbrows 132 | 133 | var moadb msdb.MySQLOauthDB 134 | moadb.DB = dbAu 135 | 136 | odbAu = &moadb 137 | 138 | var man OauthManager 139 | var l lg.Logger 140 | man.Log = &l 141 | moadb.Log = &l 142 | man.Db = odbAu 143 | var m Manager 144 | m = &man 145 | 146 | var uri ClientRedirectURI 147 | uri.URI = "test" 148 | suc := m.DeleteClientRedirectURI(5) 149 | if !suc { 150 | t.Fail() 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /managers/clientRoleManager.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | //"fmt" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //ClientRole ClientRole 30 | type ClientRole struct { 31 | ID int64 `json:"id"` 32 | Role string `json:"role"` 33 | ClientID int64 `json:"clientId"` 34 | } 35 | 36 | //AddClientRole AddClientRole 37 | func (m *OauthManager) AddClientRole(r *ClientRole) (bool, int64) { 38 | // var suc bool 39 | // var id int64 40 | var cr odb.ClientRole 41 | cr.Role = r.Role 42 | cr.ClientID = r.ClientID 43 | suc, id := m.Db.AddClientRole(&cr) 44 | return suc, id 45 | } 46 | 47 | //GetClientRoleList GetClientRoleList 48 | func (m *OauthManager) GetClientRoleList(clientID int64) *[]ClientRole { 49 | var rtn = []ClientRole{} 50 | rl := m.Db.GetClientRoleList(clientID) 51 | for _, r := range *rl { 52 | var cr ClientRole 53 | cr.ID = r.ID 54 | cr.Role = r.Role 55 | cr.ClientID = r.ClientID 56 | rtn = append(rtn, cr) 57 | } 58 | return &rtn 59 | } 60 | 61 | //DeleteClientRole DeleteClientRole 62 | func (m *OauthManager) DeleteClientRole(id int64) bool { 63 | suc := m.Db.DeleteClientRole(id) 64 | return suc 65 | } 66 | -------------------------------------------------------------------------------- /managers/clientRoleManager_test.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "testing" 5 | 6 | lg "github.com/Ulbora/Level_Logger" 7 | db "github.com/Ulbora/dbinterface" 8 | mdb "github.com/Ulbora/dbinterface_mysql" 9 | 10 | msdb "github.com/Ulbora/GoAuth2/mysqldb" 11 | odb "github.com/Ulbora/GoAuth2/oauth2database" 12 | ) 13 | 14 | func TestOauthManagerClientRoles_AddClientRole(t *testing.T) { 15 | 16 | var dbAu db.Database 17 | var odbAu odb.Oauth2DB 18 | var mydb mdb.MyDBMock 19 | mydb.Host = "localhost:3306" 20 | mydb.User = "admin" 21 | mydb.Password = "admin" 22 | mydb.Database = "ulbora_oauth2_server" 23 | dbAu = &mydb 24 | 25 | var mTestRow db.DbRow 26 | mTestRow.Row = []string{} 27 | mydb.MockTestRow = &mTestRow 28 | 29 | mydb.MockInsertID1 = 2 30 | mydb.MockInsertSuccess1 = true 31 | 32 | // var rows [][]string 33 | // row1 := []string{"1", "code", "2"} 34 | // rows = append(rows, row1) 35 | // var dbrows db.DbRows 36 | // dbrows.Rows = rows 37 | // mydb.MockRows1 = &dbrows 38 | 39 | var moadb msdb.MySQLOauthDB 40 | moadb.DB = dbAu 41 | 42 | odbAu = &moadb 43 | 44 | var man OauthManager 45 | var l lg.Logger 46 | man.Log = &l 47 | moadb.Log = &l 48 | man.Db = odbAu 49 | var m Manager 50 | m = &man 51 | 52 | var cr ClientRole 53 | cr.Role = "tester" 54 | cr.ClientID = 3 55 | suc, id := m.AddClientRole(&cr) 56 | if !suc || id != 2 { 57 | t.Fail() 58 | } 59 | } 60 | 61 | func TestOauthManagerClientRoles_GetClientRole(t *testing.T) { 62 | 63 | var dbAu db.Database 64 | var odbAu odb.Oauth2DB 65 | var mydb mdb.MyDBMock 66 | mydb.Host = "localhost:3306" 67 | mydb.User = "admin" 68 | mydb.Password = "admin" 69 | mydb.Database = "ulbora_oauth2_server" 70 | dbAu = &mydb 71 | 72 | var mTestRow db.DbRow 73 | mTestRow.Row = []string{} 74 | mydb.MockTestRow = &mTestRow 75 | 76 | // mydb.MockInsertID1 = 2 77 | // mydb.MockInsertSuccess1 = true 78 | 79 | var rows [][]string 80 | row1 := []string{"1", "role1", "2"} 81 | rows = append(rows, row1) 82 | row2 := []string{"2", "role2", "2"} 83 | rows = append(rows, row2) 84 | var dbrows db.DbRows 85 | dbrows.Rows = rows 86 | mydb.MockRows1 = &dbrows 87 | 88 | var moadb msdb.MySQLOauthDB 89 | moadb.DB = dbAu 90 | 91 | odbAu = &moadb 92 | 93 | var man OauthManager 94 | var l lg.Logger 95 | man.Log = &l 96 | moadb.Log = &l 97 | man.Db = odbAu 98 | var m Manager 99 | m = &man 100 | 101 | rl := m.GetClientRoleList(2) 102 | if len(*rl) != 2 || (*rl)[1].ID != 2 { 103 | t.Fail() 104 | } 105 | } 106 | 107 | func TestOauthManagerClientRoles_DeleteClientRole(t *testing.T) { 108 | 109 | var dbAu db.Database 110 | var odbAu odb.Oauth2DB 111 | var mydb mdb.MyDBMock 112 | mydb.Host = "localhost:3306" 113 | mydb.User = "admin" 114 | mydb.Password = "admin" 115 | mydb.Database = "ulbora_oauth2_server" 116 | dbAu = &mydb 117 | 118 | var mTestRow db.DbRow 119 | mTestRow.Row = []string{} 120 | mydb.MockTestRow = &mTestRow 121 | 122 | //mydb.MockInsertID1 = 2 123 | mydb.MockDeleteSuccess1 = true 124 | 125 | // var rows [][]string 126 | // row1 := []string{"1", "code", "2"} 127 | // rows = append(rows, row1) 128 | // var dbrows db.DbRows 129 | // dbrows.Rows = rows 130 | // mydb.MockRows1 = &dbrows 131 | 132 | var moadb msdb.MySQLOauthDB 133 | moadb.DB = dbAu 134 | 135 | odbAu = &moadb 136 | 137 | var man OauthManager 138 | var l lg.Logger 139 | man.Log = &l 140 | moadb.Log = &l 141 | man.Db = odbAu 142 | var m Manager 143 | m = &man 144 | 145 | suc := m.DeleteClientRole(2) 146 | if !suc { 147 | t.Fail() 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /managers/clientRoleUriManager.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | //"fmt" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //ClientRoleURI ClientRoleURI 30 | type ClientRoleURI struct { 31 | ClientRoleID int64 `json:"clientRoleId"` 32 | ClientAllowedURIID int64 `json:"clientAllowedUriId"` 33 | } 34 | 35 | //AddClientRoleURI AddClientRoleURI 36 | func (m *OauthManager) AddClientRoleURI(r *ClientRoleURI) bool { 37 | var ru odb.ClientRoleURI 38 | ru.ClientRoleID = r.ClientRoleID 39 | ru.ClientAllowedURIID = r.ClientAllowedURIID 40 | suc := m.Db.AddClientRoleURI(&ru) 41 | return suc 42 | 43 | } 44 | 45 | //GetClientRoleAllowedURIList GetClientRoleAllowedURIList 46 | func (m *OauthManager) GetClientRoleAllowedURIList(roleID int64) *[]ClientRoleURI { 47 | var rtn = []ClientRoleURI{} 48 | rul := m.Db.GetClientRoleAllowedURIList(roleID) 49 | for _, ru := range *rul { 50 | var r ClientRoleURI 51 | r.ClientRoleID = ru.ClientRoleID 52 | r.ClientAllowedURIID = ru.ClientAllowedURIID 53 | rtn = append(rtn, r) 54 | } 55 | return &rtn 56 | } 57 | 58 | //DeleteClientRoleURI DeleteClientRoleURI 59 | func (m *OauthManager) DeleteClientRoleURI(r *ClientRoleURI) bool { 60 | var ru odb.ClientRoleURI 61 | ru.ClientRoleID = r.ClientRoleID 62 | ru.ClientAllowedURIID = r.ClientAllowedURIID 63 | suc := m.Db.DeleteClientRoleURI(&ru) 64 | return suc 65 | } 66 | -------------------------------------------------------------------------------- /managers/clientRoleUriManager_test.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "testing" 5 | 6 | lg "github.com/Ulbora/Level_Logger" 7 | db "github.com/Ulbora/dbinterface" 8 | mdb "github.com/Ulbora/dbinterface_mysql" 9 | 10 | msdb "github.com/Ulbora/GoAuth2/mysqldb" 11 | odb "github.com/Ulbora/GoAuth2/oauth2database" 12 | ) 13 | 14 | func TestOauthManagerRoleURI_AddClientRoleURI(t *testing.T) { 15 | 16 | var dbAu db.Database 17 | var odbAu odb.Oauth2DB 18 | var mydb mdb.MyDBMock 19 | mydb.Host = "localhost:3306" 20 | mydb.User = "admin" 21 | mydb.Password = "admin" 22 | mydb.Database = "ulbora_oauth2_server" 23 | dbAu = &mydb 24 | 25 | var mTestRow db.DbRow 26 | mTestRow.Row = []string{} 27 | mydb.MockTestRow = &mTestRow 28 | 29 | //mydb.MockInsertID1 = 2 30 | mydb.MockInsertSuccess1 = true 31 | 32 | // var rows [][]string 33 | // row1 := []string{"1", "code", "2"} 34 | // rows = append(rows, row1) 35 | // var dbrows db.DbRows 36 | // dbrows.Rows = rows 37 | // mydb.MockRows1 = &dbrows 38 | 39 | var moadb msdb.MySQLOauthDB 40 | moadb.DB = dbAu 41 | 42 | odbAu = &moadb 43 | 44 | var man OauthManager 45 | var l lg.Logger 46 | man.Log = &l 47 | moadb.Log = &l 48 | man.Db = odbAu 49 | var m Manager 50 | m = &man 51 | var cr ClientRoleURI 52 | cr.ClientRoleID = 1 53 | cr.ClientAllowedURIID = 2 54 | suc := m.AddClientRoleURI(&cr) 55 | if !suc { 56 | t.Fail() 57 | } 58 | 59 | } 60 | 61 | func TestOauthManagerRoleURI_GetClientRoleURIList(t *testing.T) { 62 | 63 | var dbAu db.Database 64 | var odbAu odb.Oauth2DB 65 | var mydb mdb.MyDBMock 66 | mydb.Host = "localhost:3306" 67 | mydb.User = "admin" 68 | mydb.Password = "admin" 69 | mydb.Database = "ulbora_oauth2_server" 70 | dbAu = &mydb 71 | 72 | var mTestRow db.DbRow 73 | mTestRow.Row = []string{} 74 | mydb.MockTestRow = &mTestRow 75 | 76 | //mydb.MockInsertID1 = 2 77 | //mydb.MockInsertSuccess1 = true 78 | 79 | var rows [][]string 80 | row1 := []string{"2", "2"} 81 | rows = append(rows, row1) 82 | row2 := []string{"2", "3"} 83 | rows = append(rows, row2) 84 | var dbrows db.DbRows 85 | dbrows.Rows = rows 86 | mydb.MockRows1 = &dbrows 87 | 88 | var moadb msdb.MySQLOauthDB 89 | moadb.DB = dbAu 90 | 91 | odbAu = &moadb 92 | 93 | var man OauthManager 94 | var l lg.Logger 95 | man.Log = &l 96 | moadb.Log = &l 97 | man.Db = odbAu 98 | var m Manager 99 | m = &man 100 | 101 | rul := m.GetClientRoleAllowedURIList(2) 102 | if len(*rul) != 2 || (*rul)[1].ClientAllowedURIID != 3 { 103 | t.Fail() 104 | } 105 | } 106 | 107 | func TestOauthManagerRoleURI_DeleteClientRoleURI(t *testing.T) { 108 | 109 | var dbAu db.Database 110 | var odbAu odb.Oauth2DB 111 | var mydb mdb.MyDBMock 112 | mydb.Host = "localhost:3306" 113 | mydb.User = "admin" 114 | mydb.Password = "admin" 115 | mydb.Database = "ulbora_oauth2_server" 116 | dbAu = &mydb 117 | 118 | var mTestRow db.DbRow 119 | mTestRow.Row = []string{} 120 | mydb.MockTestRow = &mTestRow 121 | 122 | //mydb.MockInsertID1 = 2 123 | mydb.MockDeleteSuccess1 = true 124 | 125 | // var rows [][]string 126 | // row1 := []string{"1", "code", "2"} 127 | // rows = append(rows, row1) 128 | // var dbrows db.DbRows 129 | // dbrows.Rows = rows 130 | // mydb.MockRows1 = &dbrows 131 | 132 | var moadb msdb.MySQLOauthDB 133 | moadb.DB = dbAu 134 | 135 | odbAu = &moadb 136 | 137 | var man OauthManager 138 | var l lg.Logger 139 | man.Log = &l 140 | moadb.Log = &l 141 | man.Db = odbAu 142 | var m Manager 143 | m = &man 144 | var cr ClientRoleURI 145 | cr.ClientRoleID = 1 146 | cr.ClientAllowedURIID = 2 147 | suc := m.DeleteClientRoleURI(&cr) 148 | if !suc { 149 | t.Fail() 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /managers/grantTypeManager.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | //"fmt" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //ClientGrantType ClientGrantType 30 | type ClientGrantType struct { 31 | ID int64 `json:"id"` 32 | GrantType string `json:"grantType"` 33 | ClientID int64 `json:"clientId"` 34 | } 35 | 36 | //AddClientGrantType AddClientGrantType 37 | func (m *OauthManager) AddClientGrantType(gt *ClientGrantType) (bool, int64) { 38 | var cgt odb.ClientGrantType 39 | cgt.GrantType = gt.GrantType 40 | cgt.ClientID = gt.ClientID 41 | suc, id := m.Db.AddClientGrantType(&cgt) 42 | return suc, id 43 | } 44 | 45 | func (m *OauthManager) grantTypeTurnedOn(clientID int64, grantType string) bool { 46 | var rtn bool 47 | gtlist := m.Db.GetClientGrantTypeList(clientID) 48 | for _, gt := range *gtlist { 49 | if gt.GrantType == grantType { 50 | rtn = true 51 | } 52 | } 53 | return rtn 54 | } 55 | 56 | //GetClientGrantTypeList GetClientGrantTypeList 57 | func (m *OauthManager) GetClientGrantTypeList(clientID int64) *[]ClientGrantType { 58 | var rtn = []ClientGrantType{} 59 | gtl := m.Db.GetClientGrantTypeList(clientID) 60 | for _, gt := range *gtl { 61 | var g ClientGrantType 62 | g.ID = gt.ID 63 | g.GrantType = gt.GrantType 64 | g.ClientID = gt.ClientID 65 | rtn = append(rtn, g) 66 | } 67 | return &rtn 68 | } 69 | 70 | //DeleteClientGrantType DeleteClientGrantType 71 | func (m *OauthManager) DeleteClientGrantType(id int64) bool { 72 | suc := m.Db.DeleteClientGrantType(id) 73 | return suc 74 | } 75 | -------------------------------------------------------------------------------- /managers/jwt.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "time" 25 | 26 | jwt "github.com/gbrlsnchs/jwt/v3" 27 | ) 28 | 29 | //Payload Payload 30 | type Payload struct { 31 | TokenType string 32 | UserID string 33 | ClientID int64 34 | Subject string 35 | Issuer string 36 | Audience string 37 | ExpiresInMinute time.Duration 38 | Grant string 39 | SecretKey string 40 | RoleURIs []RoleURI 41 | ScopeList []string 42 | } 43 | 44 | //JwtPayload JwtPayload 45 | type jwtPayload struct { 46 | Payload jwt.Payload 47 | TokenType string `json:"tokenType,omitempty"` 48 | UserID string `json:"userId,omitempty"` 49 | ClientID int64 `json:"clientId,omitempty"` 50 | Grant string `json:"grant,omitempty"` 51 | RoleURIs []RoleURI `json:"roleURIs,omitempty"` 52 | ScopeList []string `json:"scopeList,omitempty"` 53 | } 54 | 55 | //RoleURI RoleURI 56 | type RoleURI struct { 57 | ClientRoleID int64 58 | Role string 59 | ClientAllowedURIID int64 60 | ClientAllowedURI string 61 | ClientID int64 62 | } 63 | 64 | //GenerateJwtToken GenerateJwtToken 65 | func (m *OauthManager) GenerateJwtToken(pl *Payload) string { 66 | var rtn string 67 | now := time.Now() 68 | var jwtPl jwt.Payload 69 | jwtPl.Subject = pl.Subject 70 | jwtPl.Issuer = pl.Issuer 71 | jwtPl.Audience = jwt.Audience{pl.Audience} 72 | jwtPl.ExpirationTime = jwt.NumericDate(now.Add(pl.ExpiresInMinute * time.Minute)) 73 | jwtPl.IssuedAt = jwt.NumericDate(now) 74 | 75 | var jpl jwtPayload 76 | jpl.Payload = jwtPl 77 | jpl.TokenType = pl.TokenType 78 | if pl.UserID != "" { 79 | jpl.UserID = pl.UserID 80 | } 81 | jpl.ClientID = pl.ClientID 82 | if pl.Grant != "" { 83 | jpl.Grant = pl.Grant 84 | } 85 | jpl.RoleURIs = pl.RoleURIs 86 | jpl.ScopeList = pl.ScopeList 87 | 88 | hs := jwt.NewHS256([]byte(pl.SecretKey)) 89 | token, err := jwt.Sign(jpl, hs) 90 | if err == nil { 91 | rtn = string(token) 92 | } 93 | return rtn 94 | } 95 | 96 | //ValidateJwt ValidateJwt 97 | func (m *OauthManager) ValidateJwt(token string, secret string) (bool, *Payload) { 98 | var valid bool 99 | var rtn Payload 100 | var pl jwtPayload 101 | now := time.Now() 102 | var hs = jwt.NewHS256([]byte(secret)) 103 | //aud = jwt.Audience{"https://golang.org"} 104 | 105 | // Validate claims "iat", "exp" and "aud". 106 | iatValidator := jwt.IssuedAtValidator(now) 107 | expValidator := jwt.ExpirationTimeValidator(now) 108 | //audValidator = jwt.AudienceValidator(aud) 109 | 110 | // Use jwt.ValidatePayload to build a jwt.VerifyOption. 111 | // Validators are run in the order informed. 112 | //pl CustomPayload 113 | validatePayload := jwt.ValidatePayload(&pl.Payload, iatValidator, expValidator) 114 | _, err := jwt.Verify([]byte(token), hs, &pl, validatePayload) 115 | if err == nil { 116 | valid = true 117 | } 118 | 119 | //fmt.Println("pl: ", pl) 120 | rtn.TokenType = pl.TokenType 121 | rtn.UserID = pl.UserID 122 | rtn.ClientID = pl.ClientID 123 | rtn.Subject = pl.Payload.Subject 124 | rtn.Issuer = pl.Payload.Issuer 125 | aud := pl.Payload.Audience 126 | for _, a := range aud { 127 | rtn.Audience = a 128 | break 129 | } 130 | rtn.Grant = pl.Grant 131 | rtn.RoleURIs = pl.RoleURIs 132 | rtn.ScopeList = pl.ScopeList 133 | 134 | return valid, &rtn 135 | } 136 | -------------------------------------------------------------------------------- /managers/jwt_test.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | //"time" 7 | ) 8 | 9 | var tkn string 10 | 11 | var tknExp string 12 | 13 | func TestOauthManagerJwt_GenerateJwtToken(t *testing.T) { 14 | 15 | var man OauthManager 16 | var pl Payload 17 | pl.TokenType = "test" 18 | pl.UserID = "tester1" 19 | pl.ClientID = 234 20 | pl.Subject = "code" 21 | pl.Issuer = "GoAuth2" 22 | pl.Audience = "GoAuth2.com" 23 | pl.ExpiresInMinute = 600 //(600 * time.Minute) => (600 * 60) => 36000 minutes => 10 hours 24 | pl.Grant = "code" 25 | pl.SecretKey = "secret" 26 | var ruList []RoleURI 27 | var ru1 RoleURI 28 | ru1.ClientRoleID = 1 29 | ru1.Role = "user" 30 | ru1.ClientAllowedURIID = 2 31 | ru1.ClientAllowedURI = "test.com" 32 | ru1.ClientID = 5 33 | ruList = append(ruList, ru1) 34 | 35 | var ru2 RoleURI 36 | ru2.ClientRoleID = 12 37 | ru2.Role = "user" 38 | ru2.ClientAllowedURIID = 21 39 | ru2.ClientAllowedURI = "test2.com" 40 | ru2.ClientID = 5 41 | ruList = append(ruList, ru2) 42 | 43 | pl.RoleURIs = ruList 44 | 45 | pl.ScopeList = []string{"web", "sever"} 46 | token := man.GenerateJwtToken(&pl) 47 | fmt.Println(token) 48 | if token == "" { 49 | t.Fail() 50 | } else { 51 | tkn = token 52 | } 53 | } 54 | 55 | func TestOauthManagerJwt_GenerateJwtTokenExp(t *testing.T) { 56 | 57 | var man OauthManager 58 | var pl Payload 59 | pl.TokenType = "test" 60 | pl.UserID = "tester1" 61 | pl.ClientID = 234 62 | pl.Subject = "code" 63 | pl.Issuer = "GoAuth2" 64 | pl.Audience = "GoAuth2.com" 65 | pl.ExpiresInMinute = 1 //(600 * time.Minute) => (600 * 60) => 36000 minutes => 10 hours 66 | pl.Grant = "code" 67 | pl.SecretKey = "secret" 68 | var ruList []RoleURI 69 | var ru1 RoleURI 70 | ru1.ClientRoleID = 1 71 | ru1.Role = "user" 72 | ru1.ClientAllowedURIID = 2 73 | ru1.ClientAllowedURI = "test.com" 74 | ru1.ClientID = 5 75 | ruList = append(ruList, ru1) 76 | 77 | var ru2 RoleURI 78 | ru2.ClientRoleID = 12 79 | ru2.Role = "user" 80 | ru2.ClientAllowedURIID = 21 81 | ru2.ClientAllowedURI = "test2.com" 82 | ru2.ClientID = 5 83 | ruList = append(ruList, ru2) 84 | 85 | pl.RoleURIs = ruList 86 | 87 | pl.ScopeList = []string{"web", "sever"} 88 | token := man.GenerateJwtToken(&pl) 89 | fmt.Println("expiring Token:", token) 90 | if token == "" { 91 | t.Fail() 92 | } else { 93 | tknExp = token 94 | } 95 | } 96 | 97 | func TestOauthManagerJwt_ValidateJwtToken(t *testing.T) { 98 | var man OauthManager 99 | 100 | suc, pl := man.ValidateJwt(tkn, "secret") 101 | if !suc || pl.TokenType != "test" || pl.UserID != "tester1" || pl.ClientID != 234 { 102 | t.Fail() 103 | } else { 104 | fmt.Println("pl: ", pl) 105 | fmt.Println("Audience: ", pl.Audience) 106 | } 107 | 108 | } 109 | 110 | // func TestOauthManagerJwt_ValidateJwtTokenExpired(t *testing.T) { 111 | // time.Sleep(2 * time.Minute) 112 | // var man OauthManager 113 | 114 | // suc, pl := man.ValidateJwt(tknExp, "secret") 115 | // fmt.Println("suc: ", suc) 116 | // if suc || pl.TokenType != "test" || pl.UserID != "tester1" || pl.ClientID != 234 { 117 | // t.Fail() 118 | // } else { 119 | // fmt.Println("pl: ", pl) 120 | // fmt.Println("Audience: ", pl.Audience) 121 | // } 122 | 123 | // } 124 | -------------------------------------------------------------------------------- /managers/loginManager.go: -------------------------------------------------------------------------------- 1 | //Package managers ... 2 | package managers 3 | 4 | /* 5 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 6 | All rights reserved. 7 | 8 | Copyright (C) 2019 Ken Williamson 9 | All rights reserved. 10 | 11 | This program is free software: you can redistribute it and/or modify 12 | it under the terms of the GNU General Public License as published by 13 | the Free Software Foundation, either version 3 of the License, or 14 | (at your option) any later version. 15 | This program is distributed in the hope that it will be useful, 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | GNU General Public License for more details. 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | 22 | */ 23 | import ( 24 | au "github.com/Ulbora/auth_interface" 25 | ) 26 | 27 | //px "github.com/Ulbora/GoProxy" 28 | 29 | //Login Login 30 | type Login struct { 31 | Username string `json:"username"` 32 | Password string `json:"password"` 33 | ClientID int64 `json:"clientId"` 34 | } 35 | 36 | //LoginRes LoginRes 37 | type LoginRes struct { 38 | Valid bool `json:"valid"` 39 | Code string `json:"code"` 40 | } 41 | 42 | //UserLogin UserLogin 43 | func (m *OauthManager) UserLogin(login *au.Login) bool { 44 | rtn := m.AuthService.UserLogin(login) 45 | return rtn 46 | } 47 | -------------------------------------------------------------------------------- /managers/loginManager_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package managers 4 | 5 | import ( 6 | "testing" 7 | 8 | "fmt" 9 | 10 | px "github.com/Ulbora/GoProxy" 11 | lg "github.com/Ulbora/Level_Logger" 12 | au "github.com/Ulbora/auth_interface" 13 | dau "github.com/Ulbora/default_auth" 14 | ) 15 | 16 | func TestOauthManagerLoginInt_UserLogin(t *testing.T) { 17 | var gp px.GoProxy 18 | var da dau.DefaultAuth 19 | var authURL = "http://localhost:3001/rs/user/login" 20 | da.AuthServerURL = authURL 21 | da.Proxy = gp.GetNewProxy() 22 | var man OauthManager 23 | var ll lg.Logger 24 | man.Log = &ll 25 | //moadb.Log = &l 26 | man.AuthService = da.GetNew() 27 | //man.Proxy = gp.GetNewProxy() 28 | var m Manager 29 | m = &man 30 | var l au.Login 31 | l.Username = "admin" 32 | l.Password = "admin" 33 | l.ClientID = 10 34 | suc := m.UserLogin(&l) 35 | fmt.Println("suc: ", suc) 36 | if !suc { 37 | t.Fail() 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /managers/loginManager_test.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "testing" 9 | 10 | px "github.com/Ulbora/GoProxy" 11 | au "github.com/Ulbora/auth_interface" 12 | dau "github.com/Ulbora/default_auth" 13 | ) 14 | 15 | func TestOauthManagerLogin_UserLogin(t *testing.T) { 16 | // var gp px.MockGoProxy 17 | // gp.MockDoSuccess1 = true 18 | // gp.MockRespCode = 200 19 | var proxy px.GoProxy 20 | var da dau.MockDefaultAuth 21 | da.MockValid = true 22 | da.AuthServerURL = authenticationServiceLocal 23 | da.Proxy = proxy.GetNewProxy() 24 | //ai := da.GetNew() 25 | 26 | var res http.Response 27 | res.Body = ioutil.NopCloser(bytes.NewBufferString(`{"valid":true, "code":"1"}`)) 28 | //gp.MockResp = &res 29 | var man OauthManager 30 | man.AuthService = da.GetNew() 31 | //man.Proxy = gp.GetNewProxy() 32 | var m Manager 33 | m = &man 34 | var l au.Login 35 | l.Username = "admin" 36 | l.Password = "admin" 37 | l.ClientID = 10 38 | suc := m.UserLogin(&l) 39 | fmt.Println("suc: ", suc) 40 | if !suc { 41 | t.Fail() 42 | } 43 | } 44 | 45 | // func TestOauthManagerLogin_UserLoginEnvBadUrl(t *testing.T) { 46 | // os.Setenv("AUTHENTICATION_SERVICE", "://localhost:3001/rs/user/login") 47 | // // var gp px.MockGoProxy 48 | // // gp.MockDoSuccess1 = true 49 | // // gp.MockRespCode = 200 50 | // var proxy px.GoProxy 51 | // var da dau.MockDefaultAuth 52 | // da.MockValid = false 53 | // da.AuthServerURL = authenticationServiceLocal 54 | // da.Proxy = proxy.GetNewProxy() 55 | // var res http.Response 56 | // res.Body = ioutil.NopCloser(bytes.NewBufferString(`{"valid":true, "code":"1"}`)) 57 | // //gp.MockResp = &res 58 | // var man OauthManager 59 | // man.AuthService = da.GetNew() 60 | // //man.Proxy = gp.GetNewProxy() 61 | // var m Manager 62 | // m = &man 63 | // var l au.Login 64 | // l.Username = "ken" 65 | // l.Password = "ken" 66 | // l.ClientID = 1 67 | // suc := m.UserLogin(&l) 68 | // if suc { 69 | // t.Fail() 70 | // } 71 | // } 72 | -------------------------------------------------------------------------------- /managers/refreshTokenManager.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | //GenerateRefreshToken GenerateRefreshToken 24 | func (m *OauthManager) GenerateRefreshToken(clientID int64, userID string, grantType string) string { 25 | var token string 26 | m.Log.Debug("m.TokenParams: ", m.TokenParams) 27 | var theKey string 28 | var theIssuer string 29 | var theAudience string 30 | if m.TokenParams != nil && m.TokenParams.RefreshTokenKey != "" { 31 | theKey = m.TokenParams.RefreshTokenKey 32 | } else { 33 | theKey = m.Db.GetRefreshTokenKey() 34 | } 35 | if m.TokenParams != nil && m.TokenParams.Issuer != "" { 36 | theIssuer = m.TokenParams.Issuer 37 | } else { 38 | theIssuer = tokenIssuer 39 | } 40 | if m.TokenParams != nil && m.TokenParams.Audience != "" { 41 | theAudience = m.TokenParams.Audience 42 | } else { 43 | theAudience = tokenAudience 44 | } 45 | //rtk := m.Db.GetRefreshTokenKey() 46 | //if rtk != "" { 47 | // here write code to generate refresh token 48 | var pl Payload 49 | pl.TokenType = refreshTokenType 50 | pl.UserID = userID 51 | pl.ClientID = clientID 52 | pl.Subject = grantType 53 | pl.Issuer = theIssuer 54 | pl.Audience = theAudience 55 | pl.ExpiresInMinute = refreshTokenLifeInMinutes //(600 * time.Minute) => (600 * 60) => 36000 minutes => 10 hours 56 | pl.Grant = grantType 57 | pl.SecretKey = theKey 58 | token = m.GenerateJwtToken(&pl) 59 | //} 60 | return token 61 | } 62 | -------------------------------------------------------------------------------- /managers/refreshTokenManager_test.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | lg "github.com/Ulbora/Level_Logger" 8 | db "github.com/Ulbora/dbinterface" 9 | mdb "github.com/Ulbora/dbinterface_mysql" 10 | 11 | msdb "github.com/Ulbora/GoAuth2/mysqldb" 12 | odb "github.com/Ulbora/GoAuth2/oauth2database" 13 | ) 14 | 15 | //var token string 16 | 17 | func TestOauthManagerRefToken_GenerateRefreshToken(t *testing.T) { 18 | 19 | var dbAu db.Database 20 | var odbAu odb.Oauth2DB 21 | var mydb mdb.MyDBMock 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbAu = &mydb 27 | 28 | var mTestRow db.DbRow 29 | mTestRow.Row = []string{} 30 | mydb.MockTestRow = &mTestRow 31 | 32 | var mGetRow db.DbRow 33 | mGetRow.Row = []string{"2", "6g651dfg6gf6"} 34 | mydb.MockRow1 = &mGetRow 35 | 36 | var moadb msdb.MySQLOauthDB 37 | moadb.DB = dbAu 38 | 39 | odbAu = &moadb 40 | 41 | var m OauthManager 42 | var l lg.Logger 43 | m.Log = &l 44 | moadb.Log = &l 45 | m.Db = odbAu 46 | token := m.GenerateRefreshToken(125, "tester1", "code") 47 | if token == "" { 48 | t.Fail() 49 | } else { 50 | fmt.Println("refreshToken in test: ", token) 51 | } 52 | 53 | valid, val := m.ValidateJwt(token, "6g651dfg6gf6") 54 | if !valid || val.UserID != "tester1" { 55 | t.Fail() 56 | } 57 | } 58 | 59 | func TestOauthManagerRefToken_GenerateRefreshTokenEnvVar(t *testing.T) { 60 | 61 | var dbAu db.Database 62 | var odbAu odb.Oauth2DB 63 | var mydb mdb.MyDBMock 64 | mydb.Host = "localhost:3306" 65 | mydb.User = "admin" 66 | mydb.Password = "admin" 67 | mydb.Database = "ulbora_oauth2_server" 68 | dbAu = &mydb 69 | 70 | var mTestRow db.DbRow 71 | mTestRow.Row = []string{} 72 | mydb.MockTestRow = &mTestRow 73 | 74 | var mGetRow db.DbRow 75 | mGetRow.Row = []string{"2", "6g651dfg6gf6"} 76 | mydb.MockRow1 = &mGetRow 77 | 78 | var moadb msdb.MySQLOauthDB 79 | moadb.DB = dbAu 80 | 81 | odbAu = &moadb 82 | 83 | var m OauthManager 84 | var tkp TokenParams 85 | tkp.RefreshTokenKey = "12345" 86 | tkp.Issuer = "testIss" 87 | tkp.Audience = "testAud" 88 | m.TokenParams = &tkp 89 | var l lg.Logger 90 | m.Log = &l 91 | moadb.Log = &l 92 | m.Db = odbAu 93 | token := m.GenerateRefreshToken(125, "tester1", "code") 94 | if token == "" { 95 | t.Fail() 96 | } else { 97 | fmt.Println("refreshToken in test: ", token) 98 | } 99 | 100 | valid, val := m.ValidateJwt(token, "12345") 101 | if !valid || val.UserID != "tester1" { 102 | t.Fail() 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /managers/runinttest.sh: -------------------------------------------------------------------------------- 1 | go test -tags=integration -------------------------------------------------------------------------------- /managers/utilities.go: -------------------------------------------------------------------------------- 1 | package managers 2 | 3 | import ( 4 | "math/rand" 5 | "strconv" 6 | "time" 7 | ) 8 | 9 | /* 10 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 11 | All rights reserved. 12 | 13 | Copyright (C) 2019 Ken Williamson 14 | All rights reserved. 15 | 16 | This program is free software: you can redistribute it and/or modify 17 | it under the terms of the GNU General Public License as published by 18 | the Free Software Foundation, either version 3 of the License, or 19 | (at your option) any later version. 20 | This program is distributed in the hope that it will be useful, 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | GNU General Public License for more details. 24 | You should have received a copy of the GNU General Public License 25 | along with this program. If not, see . 26 | 27 | */ 28 | 29 | const ( 30 | shifter = 4 31 | ) 32 | 33 | func generateRandonAuthCode() string { 34 | var text = "" 35 | var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 36 | for i := 0; i < 20; i++ { 37 | rand.Seed(time.Now().UnixNano()) 38 | t := possible[rand.Intn(len(possible))] 39 | text += string(t) 40 | } 41 | return text 42 | } 43 | 44 | func generateAuthCodeString(authCode int64, randCode string) string { 45 | var rtn string 46 | if authCode != 0 && randCode != "" { 47 | fpart := string(randCode[0:8]) 48 | spart := string(randCode[8:]) 49 | cpart := strconv.FormatInt(authCode, 10) 50 | rtn = fpart + cpart + spart 51 | //fmt.Println("old authcode string: ", randCode) 52 | //fmt.Println("new authcode string: ", rtn) 53 | } 54 | 55 | return rtn 56 | } 57 | 58 | func generateClientSecret() string { 59 | var text = "" 60 | var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 61 | for i := 0; i < 50; i++ { 62 | rand.Seed(time.Now().UnixNano()) 63 | t := possible[rand.Intn(len(possible))] 64 | text += string(t) 65 | } 66 | return text 67 | } 68 | 69 | func hashUser(username string) string { 70 | var rtn string 71 | for i := 0; i < len(username); i++ { 72 | //fmt.Println("username[i]: ", username[i]) 73 | c := username[i] 74 | //fmt.Println("c before: ", c) 75 | c += shifter 76 | //fmt.Println("c: ", c) 77 | char := string(c) 78 | //fmt.Println("char: ", char) 79 | rtn += char 80 | } 81 | return rtn 82 | } 83 | 84 | func unHashUser(username string) string { 85 | var rtn string 86 | for i := 0; i < len(username); i++ { 87 | //fmt.Println("username[i]: ", username[i]) 88 | c := username[i] 89 | //fmt.Println("c before: ", c) 90 | c -= shifter 91 | //fmt.Println("c: ", c) 92 | char := string(c) 93 | //fmt.Println("char: ", char) 94 | rtn += char 95 | } 96 | return rtn 97 | } 98 | -------------------------------------------------------------------------------- /managers/utilities_test.go: -------------------------------------------------------------------------------- 1 | //Package managers ... 2 | package managers 3 | 4 | import ( 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | func Test_generateClientSecret(t *testing.T) { 10 | sec := generateClientSecret() 11 | fmt.Println("secret :", sec) 12 | if len(sec) < 50 { 13 | t.Fail() 14 | } 15 | } 16 | 17 | func Test_generateRandonAuthCode(t *testing.T) { 18 | sec := generateClientSecret() 19 | fmt.Println("random :", sec) 20 | if len(sec) < 20 { 21 | t.Fail() 22 | } 23 | } 24 | 25 | func Test_hashUser(t *testing.T) { 26 | h := hashUser("kenz") 27 | fmt.Println("hash :", h) 28 | if h != "oir~" { 29 | t.Fail() 30 | } 31 | } 32 | 33 | func Test_unHashUser(t *testing.T) { 34 | h := unHashUser("oir~") 35 | fmt.Println("hash :", h) 36 | if h != "kenz" { 37 | t.Fail() 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /mysqldb/accessToken.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | "time" 26 | 27 | odb "github.com/Ulbora/GoAuth2/oauth2database" 28 | dbtx "github.com/Ulbora/dbinterface" 29 | ) 30 | 31 | //AddAccessToken AddAccessToken 32 | func (d *MySQLOauthDB) AddAccessToken(tx dbtx.Transaction, t *odb.AccessToken) (bool, int64) { 33 | if tx == nil && !d.testConnection() { 34 | d.DB.Connect() 35 | } 36 | var suc bool 37 | var id int64 38 | var a []interface{} 39 | if tx == nil && t.RefreshTokenID == 0 { 40 | a = append(a, t.Token, t.Expires) 41 | suc, id = d.DB.Insert(insertAccessTokenNull, a...) 42 | } else if tx == nil { 43 | a = append(a, t.Token, t.Expires, t.RefreshTokenID) 44 | suc, id = d.DB.Insert(insertAccessToken, a...) 45 | } else if t.RefreshTokenID == 0 { 46 | a = append(a, t.Token, t.Expires) 47 | suc, id = tx.Insert(insertAccessTokenNull, a...) 48 | } else { 49 | a = append(a, t.Token, t.Expires, t.RefreshTokenID) 50 | suc, id = tx.Insert(insertAccessToken, a...) 51 | } 52 | 53 | return suc, id 54 | } 55 | 56 | //UpdateAccessToken UpdateAccessToken 57 | func (d *MySQLOauthDB) UpdateAccessToken(tx dbtx.Transaction, t *odb.AccessToken) bool { 58 | if tx == nil && !d.testConnection() { 59 | d.DB.Connect() 60 | } 61 | var suc bool 62 | var a []interface{} 63 | if tx == nil && t.RefreshTokenID == 0 { 64 | a = append(a, t.Token, t.Expires, t.ID) 65 | suc = d.DB.Update(updateAccessTokenNull, a...) 66 | } else if tx == nil { 67 | a = append(a, t.Token, t.Expires, t.RefreshTokenID, t.ID) 68 | suc = d.DB.Update(updateAccessToken, a...) 69 | } else if t.RefreshTokenID == 0 { 70 | a = append(a, t.Token, t.Expires, t.ID) 71 | suc = tx.Update(updateAccessTokenNull, a...) 72 | } else { 73 | a = append(a, t.Token, t.Expires, t.RefreshTokenID, t.ID) 74 | suc = tx.Update(updateAccessToken, a...) 75 | } 76 | return suc 77 | } 78 | 79 | //GetAccessToken GetAccessToken 80 | func (d *MySQLOauthDB) GetAccessToken(id int64) *odb.AccessToken { 81 | if !d.testConnection() { 82 | d.DB.Connect() 83 | } 84 | var a []interface{} 85 | a = append(a, id) 86 | row := d.DB.Get(getAccessToken, a...) 87 | rtn := parseAccessTokenRow(&row.Row) 88 | return rtn 89 | } 90 | 91 | //DeleteAccessToken DeleteAccessToken 92 | func (d *MySQLOauthDB) DeleteAccessToken(tx dbtx.Transaction, id int64) bool { 93 | var suc bool 94 | if tx == nil && !d.testConnection() { 95 | d.DB.Connect() 96 | } 97 | var a []interface{} 98 | a = append(a, id) 99 | if tx == nil { 100 | suc = d.DB.Delete(deleteAccessToken, a...) 101 | } else { 102 | suc = tx.Delete(deleteAccessToken, a...) 103 | } 104 | return suc 105 | } 106 | 107 | func parseAccessTokenRow(foundRow *[]string) *odb.AccessToken { 108 | var rtn odb.AccessToken 109 | if len(*foundRow) > 0 { 110 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 111 | if err == nil { 112 | cTime, err := time.Parse(odb.TimeFormat, (*foundRow)[2]) 113 | if err == nil { 114 | rtn.ID = id 115 | rtn.Token = (*foundRow)[1] 116 | rtn.Expires = cTime 117 | if (*foundRow)[3] != "" { 118 | refTokID, _ := strconv.ParseInt((*foundRow)[3], 10, 64) 119 | rtn.RefreshTokenID = refTokID 120 | } 121 | } 122 | } 123 | } 124 | return &rtn 125 | } 126 | -------------------------------------------------------------------------------- /mysqldb/accessToken_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | odb "github.com/Ulbora/GoAuth2/oauth2database" 11 | lg "github.com/Ulbora/Level_Logger" 12 | db "github.com/Ulbora/dbinterface" 13 | mdb "github.com/Ulbora/dbinterface_mysql" 14 | ) 15 | 16 | var dbAti db.Database 17 | var odbAti odb.Oauth2DB 18 | var idAti int64 19 | var refTkIdi int64 20 | 21 | //var cidRti int64 22 | 23 | func TestMySQLOauthDBAcTokeni_Connect(t *testing.T) { 24 | //var db db.Database 25 | var mydb mdb.MyDB 26 | mydb.Host = "localhost:3306" 27 | mydb.User = "admin" 28 | mydb.Password = "admin" 29 | mydb.Database = "ulbora_oauth2_server" 30 | dbAti = &mydb 31 | 32 | var moadb MySQLOauthDB 33 | var l lg.Logger 34 | moadb.Log = &l 35 | moadb.DB = dbAti 36 | 37 | odbAti = &moadb 38 | 39 | dbAti.Connect() 40 | } 41 | 42 | func TestMySQLOauthDBAcTokeni_AddAccessToken(t *testing.T) { 43 | var tk odb.AccessToken 44 | tk.Token = "someacctoken" 45 | tk.Expires = time.Now() 46 | res, id := odbAti.AddAccessToken(nil, &tk) 47 | if !res || id <= 0 { 48 | t.Fail() 49 | } else { 50 | idAti = id 51 | } 52 | } 53 | 54 | func TestMySQLOauthDBAcTokeni_UpdateAccessToken(t *testing.T) { 55 | var tk odb.AccessToken 56 | tk.ID = idAti 57 | tk.Token = "someacctoken2" 58 | tk.Expires = time.Now() 59 | res := odbAti.UpdateAccessToken(nil, &tk) 60 | if !res { 61 | t.Fail() 62 | } 63 | } 64 | 65 | func TestMySQLOauthDBAcTokeni_GetAccessToken(t *testing.T) { 66 | res := odbAti.GetAccessToken(idAti) 67 | fmt.Println("access token: ", res) 68 | fmt.Println("access token refTokenId: ", res.RefreshTokenID) 69 | if res == nil || (*res).Token != "someacctoken2" { 70 | t.Fail() 71 | } 72 | } 73 | 74 | func TestMySQLOauthDBAcTokeni_DeleteAccessToken(t *testing.T) { 75 | res := odbAti.DeleteAccessToken(nil, idAti) 76 | fmt.Println("del access token: ", res) 77 | if !res { 78 | t.Fail() 79 | } 80 | } 81 | 82 | func TestMySQLOauthDBAcTokeni_AddRefreshToken(t *testing.T) { 83 | var tk odb.RefreshToken 84 | tk.Token = "somereftoken" 85 | res, id := odbAti.AddRefreshToken(nil, &tk) 86 | if !res || id <= 0 { 87 | t.Fail() 88 | } else { 89 | refTkIdi = id 90 | } 91 | } 92 | 93 | func TestMySQLOauthDBAcTokeni_AddAccessToken2(t *testing.T) { 94 | var tk odb.AccessToken 95 | tk.Token = "someacctoken" 96 | tk.Expires = time.Now() 97 | tk.RefreshTokenID = refTkIdi 98 | res, id := odbAti.AddAccessToken(nil, &tk) 99 | if !res || id <= 0 { 100 | t.Fail() 101 | } else { 102 | idAti = id 103 | } 104 | } 105 | 106 | func TestMySQLOauthDBAcTokeni_UpdateAccessToken2(t *testing.T) { 107 | var tk odb.AccessToken 108 | tk.ID = idAti 109 | tk.Token = "someacctoken2" 110 | tk.Expires = time.Now() 111 | tk.RefreshTokenID = refTkIdi 112 | res := odbAti.UpdateAccessToken(nil, &tk) 113 | if !res { 114 | t.Fail() 115 | } 116 | } 117 | 118 | func TestMySQLOauthDBAcTokeni_GetAccessToken2(t *testing.T) { 119 | res := odbAti.GetAccessToken(idAti) 120 | fmt.Println("access token: ", res) 121 | fmt.Println("access token refTokenId: ", res.RefreshTokenID) 122 | if res == nil || (*res).Token != "someacctoken2" { 123 | t.Fail() 124 | } 125 | } 126 | 127 | func TestMySQLOauthDBAcTokeni_DeleteAccessToken2(t *testing.T) { 128 | res := odbAti.DeleteAccessToken(nil, idAti) 129 | fmt.Println("del access token: ", res) 130 | if !res { 131 | t.Fail() 132 | } 133 | } 134 | 135 | func TestMySQLOauthDBAcTokeni_DeleteRefreshToken(t *testing.T) { 136 | res := odbAti.DeleteRefreshToken(nil, refTkIdi) 137 | fmt.Println("del ref token: ", res) 138 | if !res { 139 | t.Fail() 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /mysqldb/allowedUri.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //AddClientAllowedURI AddClientAllowedURI 30 | func (d *MySQLOauthDB) AddClientAllowedURI(au *odb.ClientAllowedURI) (bool, int64) { 31 | if !d.testConnection() { 32 | d.DB.Connect() 33 | } 34 | var a []interface{} 35 | a = append(a, au.URI, au.ClientID) 36 | suc, id := d.DB.Insert(insertAllowedURI, a...) 37 | return suc, id 38 | } 39 | 40 | //UpdateClientAllowedURI UpdateClientAllowedURI 41 | func (d *MySQLOauthDB) UpdateClientAllowedURI(au *odb.ClientAllowedURI) bool { 42 | if !d.testConnection() { 43 | d.DB.Connect() 44 | } 45 | var a []interface{} 46 | a = append(a, au.URI, au.ID) 47 | suc := d.DB.Update(updateAllowedURI, a...) 48 | return suc 49 | } 50 | 51 | //GetClientAllowedURIByID GetClientAllowedURIByID 52 | func (d *MySQLOauthDB) GetClientAllowedURIByID(id int64) *odb.ClientAllowedURI { 53 | if !d.testConnection() { 54 | d.DB.Connect() 55 | } 56 | var a []interface{} 57 | a = append(a, id) 58 | row := d.DB.Get(getAllowedURIByID, a...) 59 | rtn := parseClientAllowedURIRow(&row.Row) 60 | return rtn 61 | } 62 | 63 | //GetClientAllowedURIList GetClientAllowedURIList 64 | func (d *MySQLOauthDB) GetClientAllowedURIList(clientID int64) *[]odb.ClientAllowedURI { 65 | if !d.testConnection() { 66 | d.DB.Connect() 67 | } 68 | var rtn []odb.ClientAllowedURI 69 | var a []interface{} 70 | a = append(a, clientID) 71 | rows := d.DB.GetList(getAllowedURIList, a...) 72 | if rows != nil && len(rows.Rows) != 0 { 73 | foundRows := rows.Rows 74 | for r := range foundRows { 75 | foundRow := foundRows[r] 76 | rowContent := parseClientAllowedURIRow(&foundRow) 77 | rtn = append(rtn, *rowContent) 78 | } 79 | } 80 | return &rtn 81 | } 82 | 83 | //GetClientAllowedURI GetClientAllowedURI 84 | func (d *MySQLOauthDB) GetClientAllowedURI(clientID int64, uri string) *odb.ClientAllowedURI { 85 | if !d.testConnection() { 86 | d.DB.Connect() 87 | } 88 | var a []interface{} 89 | a = append(a, clientID, uri) 90 | row := d.DB.Get(getAllowedURI, a...) 91 | rtn := parseClientAllowedURIRow(&row.Row) 92 | return rtn 93 | } 94 | 95 | //DeleteClientAllowedURI DeleteClientAllowedURI 96 | func (d *MySQLOauthDB) DeleteClientAllowedURI(id int64) bool { 97 | if !d.testConnection() { 98 | d.DB.Connect() 99 | } 100 | var a []interface{} 101 | a = append(a, id) 102 | return d.DB.Delete(deleteAllowedURI, a...) 103 | } 104 | 105 | func parseClientAllowedURIRow(foundRow *[]string) *odb.ClientAllowedURI { 106 | var rtn odb.ClientAllowedURI 107 | if len(*foundRow) > 0 { 108 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 109 | if err == nil { 110 | clientID, err := strconv.ParseInt((*foundRow)[2], 10, 64) 111 | if err == nil { 112 | rtn.ID = id 113 | rtn.ClientID = clientID 114 | rtn.URI = (*foundRow)[1] 115 | } 116 | } 117 | } 118 | return &rtn 119 | } 120 | -------------------------------------------------------------------------------- /mysqldb/allowedUri_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbAui db.Database 16 | var odbAui odb.Oauth2DB 17 | var cidAui int64 18 | var idAui int64 19 | 20 | func TestMySQLOauthDBi_ConnectAllowURI(t *testing.T) { 21 | var mydb mdb.MyDB 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbAui = &mydb 27 | 28 | var moadb MySQLOauthDB 29 | var l lg.Logger 30 | moadb.Log = &l 31 | moadb.DB = dbAui 32 | 33 | odbAui = &moadb 34 | 35 | dbAui.Connect() 36 | } 37 | 38 | func TestMySQLOauthDBi_AddClientInAlowedUri(t *testing.T) { 39 | var c odb.Client 40 | c.Secret = "12345" 41 | c.Name = "tester" 42 | c.Email = "bob@bob.com" 43 | c.WebSite = "www.bob.com" 44 | c.Enabled = true 45 | c.Paid = false 46 | 47 | fmt.Println("before db add") 48 | res, id := odbAui.AddClient(&c, nil) 49 | fmt.Println("res: ", res) 50 | fmt.Println("id: ", id) 51 | if !res || id == 0 { 52 | t.Fail() 53 | } else { 54 | cidAui = id 55 | } 56 | } 57 | 58 | func TestMySQLOauthDBi_AddClientAllowedURI(t *testing.T) { 59 | var ur odb.ClientAllowedURI 60 | ur.ClientID = cidAui 61 | ur.URI = "someuri" 62 | res, id := odbAui.AddClientAllowedURI(&ur) 63 | if !res || id <= 0 { 64 | t.Fail() 65 | } else { 66 | idAui = id 67 | } 68 | } 69 | 70 | func TestMySQLOauthDBi_UpdateClientAllowedURI(t *testing.T) { 71 | var ur odb.ClientAllowedURI 72 | ur.ID = idAui 73 | ur.URI = "someuri2" 74 | res := odbAui.UpdateClientAllowedURI(&ur) 75 | if !res { 76 | t.Fail() 77 | } 78 | } 79 | 80 | func TestMySQLOauthDBi_GetClientAllowedURIByID(t *testing.T) { 81 | res := odbAui.GetClientAllowedURIByID(idAui) 82 | fmt.Println("allowed uri res by id: ", res) 83 | if res == nil || (*res).ClientID != cidAui || (*res).URI != "someuri2" { 84 | t.Fail() 85 | } 86 | } 87 | 88 | func TestMySQLOauthDBi_GetClientAllowedURIList(t *testing.T) { 89 | res := odbAui.GetClientAllowedURIList(cidAui) 90 | fmt.Println("allowed uri list res: ", res) 91 | if res == nil || (*res)[0].ClientID != cidAui { 92 | t.Fail() 93 | } 94 | } 95 | 96 | func TestMySQLOauthDBi_GetClientAllowedURI(t *testing.T) { 97 | res := odbAui.GetClientAllowedURI(cidAui, "someuri2") 98 | fmt.Println("allowed uri res: ", res) 99 | if res == nil || (*res).ClientID != cidAui || (*res).URI != "someuri2" { 100 | t.Fail() 101 | } 102 | } 103 | func TestMySQLOauthDBi_DeleteClientAllowedURI(t *testing.T) { 104 | res := odbAui.DeleteClientAllowedURI(idAui) 105 | fmt.Println("allowed uri delete: ", res) 106 | if !res { 107 | t.Fail() 108 | } 109 | } 110 | 111 | func TestMySQLOauthDBi_DeleteClientInAllowedURI(t *testing.T) { 112 | suc := odbAui.DeleteClient(cidAui) 113 | if !suc { 114 | t.Fail() 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /mysqldb/allowedUri_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbAu db.Database 14 | var odbAu odb.Oauth2DB 15 | var cidAu int64 16 | var idAu int64 17 | 18 | func TestMySQLOauthDB_ConnectAllowURI(t *testing.T) { 19 | var mydb mdb.MyDBMock 20 | mydb.Host = "localhost:3306" 21 | mydb.User = "admin" 22 | mydb.Password = "admin" 23 | mydb.Database = "ulbora_oauth2_server" 24 | dbAu = &mydb 25 | 26 | var mTestRow db.DbRow 27 | mTestRow.Row = []string{} 28 | mydb.MockTestRow = &mTestRow 29 | 30 | mydb.MockInsertSuccess1 = true 31 | mydb.MockInsertID1 = 1 32 | 33 | mydb.MockUpdateSuccess1 = true 34 | 35 | var getRow db.DbRow 36 | getRow.Row = []string{"1", "someuri2", "2"} 37 | mydb.MockRow1 = &getRow 38 | mydb.MockRow2 = &getRow 39 | 40 | var rows [][]string 41 | row1 := []string{"1", "tester5", "2"} 42 | rows = append(rows, row1) 43 | var dbrows db.DbRows 44 | dbrows.Rows = rows 45 | mydb.MockRows1 = &dbrows 46 | 47 | mydb.MockDeleteSuccess1 = true 48 | 49 | var moadb MySQLOauthDB 50 | var l lg.Logger 51 | moadb.Log = &l 52 | moadb.DB = dbAu 53 | 54 | odbAu = &moadb 55 | 56 | dbAu.Connect() 57 | } 58 | 59 | func TestMySQLOauthDB_AddClientAllowedURI(t *testing.T) { 60 | cidAu = 2 61 | var ur odb.ClientAllowedURI 62 | ur.ClientID = cidAu 63 | ur.URI = "someuri" 64 | res, id := odbAu.AddClientAllowedURI(&ur) 65 | if !res || id <= 0 { 66 | t.Fail() 67 | } else { 68 | idAu = id 69 | } 70 | } 71 | 72 | func TestMySQLOauthDB_UpdateClientAllowedURI(t *testing.T) { 73 | var ur odb.ClientAllowedURI 74 | ur.ID = idAu 75 | ur.URI = "someuri2" 76 | res := odbAu.UpdateClientAllowedURI(&ur) 77 | if !res { 78 | t.Fail() 79 | } 80 | } 81 | 82 | func TestMySQLOauthDB_GetClientAllowedURIByID(t *testing.T) { 83 | res := odbAu.GetClientAllowedURIByID(idAu) 84 | fmt.Println("allowed uri res by id: ", res) 85 | if res == nil || (*res).ClientID != cidAu || (*res).URI != "someuri2" { 86 | t.Fail() 87 | } 88 | } 89 | 90 | func TestMySQLOauthDB_GetClientAllowedURIList(t *testing.T) { 91 | res := odbAu.GetClientAllowedURIList(cidAu) 92 | fmt.Println("allowed uri list res: ", res) 93 | if res == nil || (*res)[0].ClientID != cidAu { 94 | t.Fail() 95 | } 96 | } 97 | 98 | func TestMySQLOauthDB_GetClientAllowedURI(t *testing.T) { 99 | res := odbAu.GetClientAllowedURI(cidAu, "someuri2") 100 | fmt.Println("allowed uri res: ", res) 101 | if res == nil || (*res).ClientID != cidAu || (*res).URI != "someuri2" { 102 | t.Fail() 103 | } 104 | } 105 | 106 | func TestMySQLOauthDB_DeleteClientAllowedURI(t *testing.T) { 107 | res := odbAu.DeleteClientAllowedURI(idAu) 108 | fmt.Println("allowed uri delete: ", res) 109 | if !res { 110 | t.Fail() 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /mysqldb/authCodeRevolk.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | dbtx "github.com/Ulbora/dbinterface" 28 | ) 29 | 30 | //AddAuthCodeRevolk AddAuthCodeRevolk 31 | func (d *MySQLOauthDB) AddAuthCodeRevolk(tx dbtx.Transaction, rv *odb.AuthCodeRevolk) (bool, int64) { 32 | var suc bool 33 | var id int64 34 | if tx == nil && !d.testConnection() { 35 | d.DB.Connect() 36 | } 37 | var a []interface{} 38 | a = append(a, rv.AuthorizationCode) 39 | if tx == nil { 40 | suc, id = d.DB.Insert(insertAuthCodeRevolk, a...) 41 | } else { 42 | suc, id = tx.Insert(insertAuthCodeRevolk, a...) 43 | } 44 | return suc, id 45 | } 46 | 47 | //GetAuthCodeRevolk GetAuthCodeRevolk 48 | func (d *MySQLOauthDB) GetAuthCodeRevolk(ac int64) *odb.AuthCodeRevolk { 49 | if !d.testConnection() { 50 | d.DB.Connect() 51 | } 52 | var a []interface{} 53 | a = append(a, ac) 54 | row := d.DB.Get(getAuthCodeRevolk, a...) 55 | d.Log.Debug("row: ", row) 56 | rtn := parseAuthCodeRevolkRow(&row.Row) 57 | return rtn 58 | } 59 | 60 | //DeleteAuthCodeRevolk DeleteAuthCodeRevolk 61 | func (d *MySQLOauthDB) DeleteAuthCodeRevolk(tx dbtx.Transaction, ac int64) bool { 62 | var suc bool 63 | if tx == nil && !d.testConnection() { 64 | d.DB.Connect() 65 | } 66 | var a []interface{} 67 | a = append(a, ac) 68 | if tx == nil { 69 | suc = d.DB.Delete(deleteAuthCodeRevolk, a...) 70 | } else { 71 | suc = tx.Delete(deleteAuthCodeRevolk, a...) 72 | } 73 | return suc 74 | } 75 | 76 | func parseAuthCodeRevolkRow(foundRow *[]string) *odb.AuthCodeRevolk { 77 | var rtn odb.AuthCodeRevolk 78 | if len(*foundRow) > 0 { 79 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 80 | if err == nil { 81 | ac, err := strconv.ParseInt((*foundRow)[1], 10, 64) 82 | if err == nil { 83 | rtn.ID = id 84 | rtn.AuthorizationCode = ac 85 | } 86 | } 87 | } 88 | 89 | return &rtn 90 | } 91 | -------------------------------------------------------------------------------- /mysqldb/authCodeRevolk_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | odb "github.com/Ulbora/GoAuth2/oauth2database" 11 | lg "github.com/Ulbora/Level_Logger" 12 | db "github.com/Ulbora/dbinterface" 13 | mdb "github.com/Ulbora/dbinterface_mysql" 14 | ) 15 | 16 | var dbAcRvi db.Database 17 | var odbAcRvi odb.Oauth2DB 18 | var cidAcRvi int64 19 | var acIDAcRvi int64 20 | var spID2AcRvi int64 21 | 22 | func TestMySQLOauthDBAcRvi_Connect(t *testing.T) { 23 | //var db db.Database 24 | var mydb mdb.MyDB 25 | mydb.Host = "localhost:3306" 26 | mydb.User = "admin" 27 | mydb.Password = "admin" 28 | mydb.Database = "ulbora_oauth2_server" 29 | dbAcRvi = &mydb 30 | 31 | var moadb MySQLOauthDB 32 | var l lg.Logger 33 | moadb.Log = &l 34 | moadb.DB = dbAcRvi 35 | 36 | odbAcRvi = &moadb 37 | 38 | dbAcRvi.Connect() 39 | } 40 | 41 | func TestMySQLOauthDBAcRvi_AddClientNullUri(t *testing.T) { 42 | var c odb.Client 43 | c.Secret = "12345" 44 | c.Name = "tester" 45 | c.Email = "bob@bob.com" 46 | c.WebSite = "www.bob.com" 47 | c.Enabled = true 48 | c.Paid = false 49 | 50 | fmt.Println("before db add") 51 | res, id := odbAcRvi.AddClient(&c, nil) 52 | fmt.Println("client add res: ", res) 53 | fmt.Println("client id: ", id) 54 | if !res || id == 0 { 55 | t.Fail() 56 | } else { 57 | cidAcRvi = id 58 | } 59 | } 60 | 61 | func TestMySQLOauthDBAcRvi_AddAuthorizationCode(t *testing.T) { 62 | 63 | var rt odb.RefreshToken 64 | rt.Token = "somereftoken2" 65 | 66 | var at odb.AccessToken 67 | at.Token = "someacctoken" 68 | at.Expires = time.Now() 69 | 70 | var ac odb.AuthorizationCode 71 | ac.ClientID = cidAcRvi 72 | ac.UserID = "1234" 73 | ac.Expires = time.Now() 74 | ac.RandonAuthCode = "13445" 75 | 76 | res, id := odbAcRvi.AddAuthorizationCode(&ac, &at, &rt, nil) 77 | 78 | if !res || id < 1 { 79 | t.Fail() 80 | } else { 81 | acIDAcRvi = id 82 | } 83 | } 84 | 85 | func TestMySQLOauthDBAcRvi_AddAuthCodeRevolk(t *testing.T) { 86 | var rv odb.AuthCodeRevolk 87 | rv.AuthorizationCode = acIDAcRvi 88 | res, id := odbAcRvi.AddAuthCodeRevolk(nil, &rv) 89 | fmt.Println("revolk id: ", id) 90 | if !res { 91 | t.Fail() 92 | } 93 | } 94 | func TestMySQLOauthDBAcRvi_GetAuthCodeRevolk(t *testing.T) { 95 | rv := odbAcRvi.GetAuthCodeRevolk(acIDAcRvi) 96 | fmt.Println("revolk : ", rv) 97 | if rv == nil { 98 | t.Fail() 99 | } 100 | } 101 | func TestMySQLOauthDBAcRvi_DeleteAuthCodeRevolk(t *testing.T) { 102 | res := odbAcRvi.DeleteAuthCodeRevolk(nil, acIDAcRvi) 103 | if !res { 104 | t.Fail() 105 | } 106 | } 107 | 108 | func TestMySQLOauthDBAcRvi_DeleteAuthorizationCode(t *testing.T) { 109 | res := odbAcRvi.DeleteAuthorizationCode(cidAcRvi, "1234") 110 | if !res { 111 | t.Fail() 112 | } 113 | } 114 | 115 | func TestMySQLOauthDBAcRvi_DeleteClient(t *testing.T) { 116 | suc := odbAcRvi.DeleteClient(cidAcRvi) 117 | if !suc { 118 | t.Fail() 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /mysqldb/authCodeRevolk_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | //"time" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbAcRv db.Database 16 | var odbAcRv odb.Oauth2DB 17 | var cidAcRv int64 = 1 18 | var acIDAcRv int64 = 2 19 | 20 | //var spID2AcRv int64 21 | 22 | func TestMySQLOauthDBAcRv_Connect(t *testing.T) { 23 | //var db db.Database 24 | var mydb mdb.MyDBMock 25 | mydb.Host = "localhost:3306" 26 | mydb.User = "admin" 27 | mydb.Password = "admin" 28 | mydb.Database = "ulbora_oauth2_server" 29 | dbAcRv = &mydb 30 | 31 | var mTestRow db.DbRow 32 | mTestRow.Row = []string{} 33 | mydb.MockTestRow = &mTestRow 34 | 35 | var getRow db.DbRow 36 | getRow.Row = []string{"1", "2"} 37 | mydb.MockRow1 = &getRow 38 | 39 | mydb.MockInsertSuccess1 = true 40 | mydb.MockInsertID1 = 1 41 | 42 | mydb.MockDeleteSuccess1 = true 43 | 44 | var moadb MySQLOauthDB 45 | var l lg.Logger 46 | moadb.Log = &l 47 | moadb.DB = dbAcRv 48 | 49 | odbAcRv = &moadb 50 | 51 | dbAcRv.Connect() 52 | } 53 | 54 | // func TestMySQLOauthDBAcRv_AddClientNullUri(t *testing.T) { 55 | // var c odb.Client 56 | // c.Secret = "12345" 57 | // c.Name = "tester" 58 | // c.Email = "bob@bob.com" 59 | // c.WebSite = "www.bob.com" 60 | // c.Enabled = true 61 | // c.Paid = false 62 | 63 | // fmt.Println("before db add") 64 | // res, id := odbAcRv.AddClient(&c, nil) 65 | // fmt.Println("client add res: ", res) 66 | // fmt.Println("client id: ", id) 67 | // if !res || id == 0 { 68 | // t.Fail() 69 | // } else { 70 | // cidAcRv = id 71 | // } 72 | // } 73 | 74 | // func TestMySQLOauthDBAcRv_AddAuthorizationCode(t *testing.T) { 75 | 76 | // var rt odb.RefreshToken 77 | // rt.Token = "somereftoken2" 78 | 79 | // var at odb.AccessToken 80 | // at.Token = "someacctoken" 81 | // at.Expires = time.Now() 82 | 83 | // var ac odb.AuthorizationCode 84 | // ac.ClientID = cidAcRv 85 | // ac.UserID = "1234" 86 | // ac.Expires = time.Now() 87 | // ac.RandonAuthCode = "13445" 88 | 89 | // res, id := odbAcRv.AddAuthorizationCode(&ac, &at, &rt, nil) 90 | 91 | // if !res || id < 1 { 92 | // t.Fail() 93 | // } else { 94 | // acIDAcRv = id 95 | // } 96 | // } 97 | 98 | func TestMySQLOauthDBAcRv_AddAuthCodeRevolk(t *testing.T) { 99 | var rv odb.AuthCodeRevolk 100 | rv.AuthorizationCode = acIDAcRv 101 | res, id := odbAcRv.AddAuthCodeRevolk(nil, &rv) 102 | fmt.Println("revolk id: ", id) 103 | if !res { 104 | t.Fail() 105 | } 106 | } 107 | 108 | func TestMySQLOauthDBAcRv_AddAuthCodeRevolkTx(t *testing.T) { 109 | 110 | var mtx mdb.MyDbTxMock 111 | var mdbx mdb.MyDBMock 112 | mdbx.MockInsertSuccess1 = true 113 | mdbx.MockInsertID1 = 1 114 | 115 | var mTestRow db.DbRow 116 | mTestRow.Row = []string{} 117 | mdbx.MockTestRow = &mTestRow 118 | 119 | mtx.MyDBMock = &mdbx 120 | var moadbtx MySQLOauthDB 121 | var l lg.Logger 122 | moadbtx.Log = &l 123 | //moadbtx.DB = &mtx 124 | var odbbUri2TX = &moadbtx 125 | 126 | var rv odb.AuthCodeRevolk 127 | rv.AuthorizationCode = acIDAcRv 128 | res, id := odbbUri2TX.AddAuthCodeRevolk(&mtx, &rv) 129 | fmt.Println("revolk id: ", id) 130 | if !res { 131 | t.Fail() 132 | } 133 | } 134 | func TestMySQLOauthDBAcRv_GetAuthCodeRevolk(t *testing.T) { 135 | rv := odbAcRv.GetAuthCodeRevolk(acIDAcRv) 136 | fmt.Println("revolk : ", rv) 137 | if rv == nil { 138 | t.Fail() 139 | } 140 | } 141 | func TestMySQLOauthDBAcRv_DeleteAuthCodeRevolk(t *testing.T) { 142 | res := odbAcRv.DeleteAuthCodeRevolk(nil, acIDAcRv) 143 | if !res { 144 | t.Fail() 145 | } 146 | } 147 | 148 | // func TestMySQLOauthDBAcRv_DeleteAuthorizationCode(t *testing.T) { 149 | // res := odbAcRv.DeleteAuthorizationCode(cidAcRv, "1234") 150 | // if !res { 151 | // t.Fail() 152 | // } 153 | // } 154 | 155 | // func TestMySQLOauthDBAcRv_DeleteClient(t *testing.T) { 156 | // suc := odbAcRv.DeleteClient(cidAcRv) 157 | // if !suc { 158 | // t.Fail() 159 | // } 160 | // } 161 | -------------------------------------------------------------------------------- /mysqldb/authCodeScope.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | dbtx "github.com/Ulbora/dbinterface" 28 | ) 29 | 30 | //AddAuthCodeScope AddAuthCodeScope 31 | func (d *MySQLOauthDB) AddAuthCodeScope(tx dbtx.Transaction, as *odb.AuthCodeScope) (bool, int64) { 32 | var suc bool 33 | var id int64 34 | if tx != nil { 35 | var a []interface{} 36 | a = append(a, as.Scope, as.AuthorizationCode) 37 | suc, id = tx.Insert(insertAuthCodeScope, a...) 38 | } 39 | return suc, id 40 | } 41 | 42 | //GetAuthorizationCodeScopeList GetAuthorizationCodeScopeList 43 | func (d *MySQLOauthDB) GetAuthorizationCodeScopeList(ac int64) *[]odb.AuthCodeScope { 44 | if !d.testConnection() { 45 | d.DB.Connect() 46 | } 47 | var rtn []odb.AuthCodeScope 48 | var a []interface{} 49 | a = append(a, ac) 50 | rows := d.DB.GetList(getAuthorizationCodeScopeList, a...) 51 | if rows != nil && len(rows.Rows) != 0 { 52 | foundRows := rows.Rows 53 | for r := range foundRows { 54 | foundRow := foundRows[r] 55 | var acs odb.AuthCodeScope 56 | if len(foundRow) > 0 { 57 | id, err := strconv.ParseInt((foundRow)[0], 10, 64) 58 | if err == nil { 59 | ac, err := strconv.ParseInt((foundRow)[2], 10, 64) 60 | if err == nil { 61 | acs.ID = id 62 | acs.Scope = (foundRow)[1] 63 | acs.AuthorizationCode = ac 64 | } 65 | } 66 | rtn = append(rtn, acs) 67 | } 68 | } 69 | } 70 | return &rtn 71 | } 72 | 73 | //DeleteAuthCodeScopeList DeleteAuthCodeScopeList 74 | func (d *MySQLOauthDB) DeleteAuthCodeScopeList(tx dbtx.Transaction, ac int64) bool { 75 | var suc bool 76 | if tx != nil { 77 | var a []interface{} 78 | a = append(a, ac) 79 | suc = tx.Delete(deleteAllAuthCodeScope, a...) 80 | } 81 | return suc 82 | } 83 | -------------------------------------------------------------------------------- /mysqldb/clientGrantType.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //AddClientGrantType AddClientGrantType 30 | func (d *MySQLOauthDB) AddClientGrantType(gt *odb.ClientGrantType) (bool, int64) { 31 | if !d.testConnection() { 32 | d.DB.Connect() 33 | } 34 | var a []interface{} 35 | a = append(a, gt.GrantType, gt.ClientID) 36 | suc, id := d.DB.Insert(insertClientGrantType, a...) 37 | return suc, id 38 | } 39 | 40 | //GetClientGrantTypeList GetClientGrantTypeList 41 | func (d *MySQLOauthDB) GetClientGrantTypeList(clientID int64) *[]odb.ClientGrantType { 42 | if !d.testConnection() { 43 | d.DB.Connect() 44 | } 45 | var rtn []odb.ClientGrantType 46 | var a []interface{} 47 | a = append(a, clientID) 48 | rows := d.DB.GetList(getClientGrantTypeList, a...) 49 | if rows != nil && len(rows.Rows) != 0 { 50 | foundRows := rows.Rows 51 | for r := range foundRows { 52 | foundRow := foundRows[r] 53 | rowContent := parseClientGrantType(&foundRow) 54 | rtn = append(rtn, *rowContent) 55 | } 56 | } 57 | return &rtn 58 | } 59 | 60 | //DeleteClientGrantType DeleteClientGrantType 61 | func (d *MySQLOauthDB) DeleteClientGrantType(id int64) bool { 62 | if !d.testConnection() { 63 | d.DB.Connect() 64 | } 65 | var a []interface{} 66 | a = append(a, id) 67 | return d.DB.Delete(deleteClientGrantType, a...) 68 | } 69 | 70 | func parseClientGrantType(foundRow *[]string) *odb.ClientGrantType { 71 | var rtn odb.ClientGrantType 72 | if len(*foundRow) > 0 { 73 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 74 | if err == nil { 75 | clientID, err := strconv.ParseInt((*foundRow)[2], 10, 64) 76 | if err == nil { 77 | rtn.ID = id 78 | rtn.ClientID = clientID 79 | rtn.GrantType = (*foundRow)[1] 80 | } 81 | } 82 | } 83 | return &rtn 84 | } 85 | -------------------------------------------------------------------------------- /mysqldb/clientGrantType_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbCgti db.Database 16 | var odbCgti odb.Oauth2DB 17 | var cidCgti int64 18 | var idCgti int64 19 | 20 | func TestMySQLOauthDBCgti_ConnectClientGrantType(t *testing.T) { 21 | var mydb mdb.MyDB 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbCgti = &mydb 27 | 28 | var moadb MySQLOauthDB 29 | var l lg.Logger 30 | moadb.Log = &l 31 | moadb.DB = dbCgti 32 | 33 | odbCgti = &moadb 34 | 35 | dbCgti.Connect() 36 | } 37 | 38 | func TestMySQLOauthDBCgti_AddClientInClientGrantType(t *testing.T) { 39 | var c odb.Client 40 | c.Secret = "12345" 41 | c.Name = "tester" 42 | c.Email = "bob@bob.com" 43 | c.WebSite = "www.bob.com" 44 | c.Enabled = true 45 | c.Paid = false 46 | 47 | fmt.Println("before db add") 48 | res, id := odbCgti.AddClient(&c, nil) 49 | fmt.Println("res: ", res) 50 | fmt.Println("id: ", id) 51 | if !res || id == 0 { 52 | t.Fail() 53 | } else { 54 | cidCgti = id 55 | } 56 | } 57 | func TestMySQLOauthDBCgti_AddClientGrantType(t *testing.T) { 58 | var ur odb.ClientGrantType 59 | ur.ClientID = cidCgti 60 | ur.GrantType = "someGrantType" 61 | res, id := odbCgti.AddClientGrantType(&ur) 62 | if !res || id <= 0 { 63 | t.Fail() 64 | } else { 65 | idCgti = id 66 | } 67 | } 68 | 69 | func TestMySQLOauthDBCgti_GetClientGrantTypeList(t *testing.T) { 70 | res := odbCgti.GetClientGrantTypeList(cidCgti) 71 | fmt.Println("grant type list res: ", res) 72 | if res == nil || (*res)[0].ClientID != cidCgti { 73 | t.Fail() 74 | } 75 | } 76 | 77 | func TestMySQLOauthDBCgti_DeleteClientGrantType(t *testing.T) { 78 | res := odbCgti.DeleteClientGrantType(idCgti) 79 | fmt.Println("client grant type delete: ", res) 80 | if !res { 81 | t.Fail() 82 | } 83 | } 84 | 85 | func TestMySQLOauthDBCgti_DeleteClientInClientGrantType(t *testing.T) { 86 | suc := odbCgti.DeleteClient(cidCgti) 87 | if !suc { 88 | t.Fail() 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /mysqldb/clientGrantType_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbCgt db.Database 14 | var odbCgt odb.Oauth2DB 15 | var cidCgt int64 = 1 16 | var idCgt int64 17 | 18 | func TestMySQLOauthDBCgt_ConnectClientGrantType(t *testing.T) { 19 | var mydb mdb.MyDBMock 20 | mydb.Host = "localhost:3306" 21 | mydb.User = "admin" 22 | mydb.Password = "admin" 23 | mydb.Database = "ulbora_oauth2_server" 24 | dbCgt = &mydb 25 | 26 | var mTestRow db.DbRow 27 | mTestRow.Row = []string{} 28 | mydb.MockTestRow = &mTestRow 29 | 30 | mydb.MockInsertSuccess1 = true 31 | mydb.MockInsertID1 = 1 32 | 33 | var rows [][]string 34 | row1 := []string{"1", "tester5", "1"} 35 | rows = append(rows, row1) 36 | var dbrows db.DbRows 37 | dbrows.Rows = rows 38 | mydb.MockRows1 = &dbrows 39 | 40 | mydb.MockDeleteSuccess1 = true 41 | 42 | var moadb MySQLOauthDB 43 | var l lg.Logger 44 | moadb.Log = &l 45 | moadb.DB = dbCgt 46 | 47 | odbCgt = &moadb 48 | 49 | dbCgt.Connect() 50 | } 51 | 52 | func TestMySQLOauthDBCgt_AddClientGrantType(t *testing.T) { 53 | var ur odb.ClientGrantType 54 | ur.ClientID = cidCgt 55 | ur.GrantType = "someGrantType" 56 | res, id := odbCgt.AddClientGrantType(&ur) 57 | if !res || id <= 0 { 58 | t.Fail() 59 | } else { 60 | idCgt = id 61 | } 62 | } 63 | 64 | func TestMySQLOauthDBCgt_GetClientGrantTypeList(t *testing.T) { 65 | res := odbCgt.GetClientGrantTypeList(cidCgt) 66 | fmt.Println("grant type list res: ", res) 67 | if res == nil || (*res)[0].ClientID != cidCgt { 68 | t.Fail() 69 | } 70 | } 71 | 72 | func TestMySQLOauthDBCgt_DeleteClientGrantType(t *testing.T) { 73 | res := odbCgt.DeleteClientGrantType(idCgt) 74 | fmt.Println("client grant type delete: ", res) 75 | if !res { 76 | t.Fail() 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /mysqldb/client_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbbi db.Database 16 | var odbbi odb.Oauth2DB 17 | var cidi int64 18 | var cid2i int64 19 | 20 | func TestMySQLDBi_Connect(t *testing.T) { 21 | 22 | //var db db.Database 23 | var mydb mdb.MyDB 24 | mydb.Host = "localhost:3306" 25 | mydb.User = "admin" 26 | mydb.Password = "admin" 27 | mydb.Database = "ulbora_oauth2_server" 28 | dbbi = &mydb 29 | 30 | var moadb MySQLOauthDB 31 | var l lg.Logger 32 | moadb.Log = &l 33 | moadb.DB = dbbi 34 | 35 | odbbi = &moadb 36 | 37 | dbbi.Connect() 38 | 39 | } 40 | 41 | func TestMySQLDBi_AddClientNullUri(t *testing.T) { 42 | var c odb.Client 43 | c.Secret = "12345" 44 | c.Name = "tester" 45 | c.Email = "bob@bob.com" 46 | c.WebSite = "www.bob.com" 47 | c.Enabled = true 48 | c.Paid = false 49 | 50 | fmt.Println("before db add") 51 | res, id := odbbi.AddClient(&c, nil) 52 | fmt.Println("res: ", res) 53 | fmt.Println("id: ", id) 54 | if !res || id == 0 { 55 | t.Fail() 56 | } else { 57 | cidi = id 58 | } 59 | } 60 | 61 | func TestMySQLDBi_AddClient(t *testing.T) { 62 | var c odb.Client 63 | c.Secret = "1234567" 64 | c.Name = "tester" 65 | c.Email = "bob@bob.com" 66 | c.WebSite = "www.bob.com" 67 | c.Enabled = true 68 | c.Paid = false 69 | var uis []odb.ClientRedirectURI 70 | var u1 odb.ClientRedirectURI 71 | u1.URI = "addSomething" 72 | uis = append(uis, u1) 73 | 74 | var u2 odb.ClientRedirectURI 75 | u2.URI = "addSomething2" 76 | uis = append(uis, u2) 77 | 78 | fmt.Println("before db add") 79 | res, id := odbbi.AddClient(&c, &uis) 80 | cid2i = id 81 | fmt.Println("res: ", res) 82 | fmt.Println("id in addclient int test: ", id) 83 | if !res || id == 0 { 84 | t.Fail() 85 | } 86 | } 87 | 88 | func TestMySQLDBi_UpdateClient(t *testing.T) { 89 | var c odb.Client 90 | c.Secret = "555555" 91 | c.Name = "tester5" 92 | c.Email = "bob5@bob.com" 93 | c.WebSite = "www.bob.com" 94 | c.Enabled = false 95 | c.Paid = false 96 | c.ClientID = cidi 97 | suc := odbbi.UpdateClient(&c) 98 | if !suc { 99 | t.Fail() 100 | } 101 | } 102 | 103 | func TestMySQLDBi_GetClient(t *testing.T) { 104 | c := odbbi.GetClient(cidi) 105 | fmt.Println("client found: ", c) 106 | if c.Name != "tester5" { 107 | t.Fail() 108 | } 109 | } 110 | 111 | func TestMySQLDBi_GetClients(t *testing.T) { 112 | cs := odbbi.GetClients() 113 | fmt.Println("client found: ", cs) 114 | for _, c := range *cs { 115 | fmt.Println("client found in getClients: ", c) 116 | } 117 | if len(*cs) == 0 { 118 | t.Fail() 119 | } 120 | 121 | } 122 | 123 | func TestMySQLDBi_SearchClients(t *testing.T) { 124 | cs := odbbi.SearchClients("test") 125 | fmt.Println("client found in search: ", cs) 126 | for _, c := range *cs { 127 | fmt.Println("client found in searchClients: ", c) 128 | } 129 | if len(*cs) == 0 { 130 | t.Fail() 131 | } 132 | 133 | } 134 | 135 | func TestMySQLDBi_DeleteClient(t *testing.T) { 136 | suc := odbbi.DeleteClient(cidi) 137 | if !suc { 138 | t.Fail() 139 | } 140 | } 141 | 142 | func TestMySQLDBi_DeleteClient2(t *testing.T) { 143 | suc := odbbi.DeleteClient(cid2i) 144 | if !suc { 145 | t.Fail() 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /mysqldb/credentialsGrant.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | //"fmt" 25 | 26 | "strconv" 27 | 28 | odb "github.com/Ulbora/GoAuth2/oauth2database" 29 | ) 30 | 31 | //AddCredentialsGrant AddCredentialsGrant 32 | func (d *MySQLOauthDB) AddCredentialsGrant(cg *odb.CredentialsGrant, at *odb.AccessToken) (bool, int64) { 33 | var suc = false 34 | var id int64 35 | if !d.testConnection() { 36 | d.DB.Connect() 37 | } 38 | tx := d.DB.BeginTransaction() 39 | atsuc, acID := d.AddAccessToken(tx, at) 40 | d.Log.Debug("atTk res: ", atsuc) 41 | d.Log.Debug("atTk id: ", acID) 42 | if atsuc { 43 | cg.AccessTokenID = acID 44 | var a []interface{} 45 | a = append(a, cg.ClientID, cg.AccessTokenID) 46 | suc, id = tx.Insert(insertCredentialsGrant, a...) 47 | d.Log.Debug("ig res: ", suc) 48 | d.Log.Debug("ig id: ", id) 49 | if suc { 50 | tx.Commit() 51 | } else { 52 | suc = false 53 | id = 0 54 | d.Log.Debug("rolling back suc: ", suc) 55 | tx.Rollback() 56 | } 57 | } else { 58 | tx.Rollback() 59 | } 60 | return suc, id 61 | } 62 | 63 | //GetCredentialsGrant GetCredentialsGrant 64 | func (d *MySQLOauthDB) GetCredentialsGrant(clientID int64) *[]odb.CredentialsGrant { 65 | if !d.testConnection() { 66 | d.DB.Connect() 67 | } 68 | var rtn []odb.CredentialsGrant 69 | var a []interface{} 70 | a = append(a, clientID) 71 | rows := d.DB.GetList(getCredentialsGrant, a...) 72 | if rows != nil && len(rows.Rows) != 0 { 73 | foundRows := rows.Rows 74 | d.Log.Debug("foundRows in getbyscope: ", foundRows) 75 | for r := range foundRows { 76 | foundRow := foundRows[r] 77 | if len(foundRow) > 0 { 78 | d.Log.Debug("foundRow in getbyscope: ", foundRow) 79 | cgID, err := strconv.ParseInt((foundRow)[0], 10, 64) 80 | if err == nil { 81 | cid, err := strconv.ParseInt((foundRow)[1], 10, 64) 82 | if err == nil { 83 | tid, err := strconv.ParseInt((foundRow)[2], 10, 64) 84 | if err == nil { 85 | var rtnc odb.CredentialsGrant 86 | rtnc.ID = cgID 87 | rtnc.ClientID = cid 88 | rtnc.AccessTokenID = tid 89 | d.Log.Debug("rtnc in getbyscope: ", rtnc) 90 | rtn = append(rtn, rtnc) 91 | } 92 | } 93 | } 94 | } 95 | } 96 | } 97 | d.Log.Debug("CredentialsGrant list: ", rtn) 98 | return &rtn 99 | } 100 | 101 | //DeleteCredentialsGrant DeleteCredentialsGrant 102 | func (d *MySQLOauthDB) DeleteCredentialsGrant(clientID int64) bool { 103 | var suc bool 104 | if !d.testConnection() { 105 | d.DB.Connect() 106 | } 107 | cgList := d.GetCredentialsGrant(clientID) 108 | d.Log.Debug("cgList: ", cgList) 109 | if len(*cgList) == 0 { 110 | suc = true 111 | } else { 112 | for _, cg := range *cgList { 113 | if cg.ID > 0 { 114 | tx := d.DB.BeginTransaction() 115 | var a []interface{} 116 | a = append(a, cg.ID) 117 | cgdel := tx.Delete(deleteCredentialsGrant, a...) 118 | d.Log.Debug("delete cg: ", cgdel) 119 | if cgdel { 120 | atdel := d.DeleteAccessToken(tx, cg.AccessTokenID) 121 | d.Log.Debug("delete AccessToken: ", atdel) 122 | if atdel { 123 | suc = true 124 | tx.Commit() 125 | } else { 126 | tx.Rollback() 127 | } 128 | } else { 129 | tx.Rollback() 130 | } 131 | } 132 | } 133 | } 134 | return suc 135 | } 136 | -------------------------------------------------------------------------------- /mysqldb/credentialsGrant_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | odb "github.com/Ulbora/GoAuth2/oauth2database" 11 | lg "github.com/Ulbora/Level_Logger" 12 | db "github.com/Ulbora/dbinterface" 13 | mdb "github.com/Ulbora/dbinterface_mysql" 14 | ) 15 | 16 | var dbCgi db.Database 17 | var odbCgi odb.Oauth2DB 18 | var cidCgi int64 19 | var spIDCgi int64 20 | var spID2Cgi int64 21 | 22 | func TestMySQLOauthDBCgi_Connect(t *testing.T) { 23 | //var db db.Database 24 | var mydb mdb.MyDB 25 | mydb.Host = "localhost:3306" 26 | mydb.User = "admin" 27 | mydb.Password = "admin" 28 | mydb.Database = "ulbora_oauth2_server" 29 | dbCgi = &mydb 30 | 31 | var moadb MySQLOauthDB 32 | var l lg.Logger 33 | moadb.Log = &l 34 | moadb.DB = dbCgi 35 | 36 | odbCgi = &moadb 37 | 38 | dbCgi.Connect() 39 | } 40 | 41 | func TestMySQLOauthDBCgi_AddClientNullUri(t *testing.T) { 42 | var c odb.Client 43 | c.Secret = "12345" 44 | c.Name = "tester" 45 | c.Email = "bob@bob.com" 46 | c.WebSite = "www.bob.com" 47 | c.Enabled = true 48 | c.Paid = false 49 | 50 | fmt.Println("before db add") 51 | res, id := odbCgi.AddClient(&c, nil) 52 | fmt.Println("client add res: ", res) 53 | fmt.Println("client id: ", id) 54 | if !res || id == 0 { 55 | t.Fail() 56 | } else { 57 | cidCgi = id 58 | } 59 | } 60 | 61 | func TestMySQLOauthDBCgi_AddCredentialsGrant(t *testing.T) { 62 | 63 | var at odb.AccessToken 64 | at.Token = "someacctoken" 65 | at.Expires = time.Now() 66 | 67 | var pwg odb.CredentialsGrant 68 | pwg.ClientID = cidCgi 69 | res, id := odbCgi.AddCredentialsGrant(&pwg, &at) 70 | if !res || id < 1 { 71 | t.Fail() 72 | } 73 | } 74 | 75 | func TestMySQLOauthDBCgi_DeleteCredentialsGrant(t *testing.T) { 76 | res := odbCgi.DeleteCredentialsGrant(cidCgi) 77 | if !res { 78 | t.Fail() 79 | } 80 | } 81 | 82 | func TestMySQLOauthDBCgi_DeleteClient(t *testing.T) { 83 | suc := odbCgi.DeleteClient(cidCgi) 84 | if !suc { 85 | t.Fail() 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /mysqldb/implicitGrantScope.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | dbtx "github.com/Ulbora/dbinterface" 28 | ) 29 | 30 | //AddImplicitGrantScope AddImplicitGrantScope 31 | func (d *MySQLOauthDB) AddImplicitGrantScope(tx dbtx.Transaction, igs *odb.ImplicitScope) (bool, int64) { 32 | var suc bool 33 | var id int64 34 | if tx != nil { 35 | var a []interface{} 36 | a = append(a, igs.Scope, igs.ImplicitGrantID) 37 | suc, id = tx.Insert(insertImplicitScope, a...) 38 | } 39 | return suc, id 40 | } 41 | 42 | //GetImplicitGrantScopeList GetImplicitGrantScopeList 43 | func (d *MySQLOauthDB) GetImplicitGrantScopeList(ig int64) *[]odb.ImplicitScope { 44 | if !d.testConnection() { 45 | d.DB.Connect() 46 | } 47 | var rtn []odb.ImplicitScope 48 | var a []interface{} 49 | a = append(a, ig) 50 | rows := d.DB.GetList(getImplicitScopeList, a...) 51 | if rows != nil && len(rows.Rows) != 0 { 52 | foundRows := rows.Rows 53 | for r := range foundRows { 54 | foundRow := foundRows[r] 55 | if len(foundRow) > 0 { 56 | var igs odb.ImplicitScope 57 | id, err := strconv.ParseInt((foundRow)[0], 10, 64) 58 | if err == nil { 59 | igid, err := strconv.ParseInt((foundRow)[2], 10, 64) 60 | if err == nil { 61 | igs.ID = id 62 | igs.Scope = (foundRow)[1] 63 | igs.ImplicitGrantID = igid 64 | } 65 | } 66 | rtn = append(rtn, igs) 67 | } 68 | } 69 | } 70 | return &rtn 71 | } 72 | 73 | //DeleteImplicitGrantScopeList DeleteImplicitGrantScopeList 74 | func (d *MySQLOauthDB) DeleteImplicitGrantScopeList(tx dbtx.Transaction, ig int64) bool { 75 | var suc bool 76 | if tx != nil { 77 | var a []interface{} 78 | a = append(a, ig) 79 | suc = tx.Delete(deleteImplicitScope, a...) 80 | } 81 | return suc 82 | } 83 | -------------------------------------------------------------------------------- /mysqldb/implicitGrant_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | odb "github.com/Ulbora/GoAuth2/oauth2database" 11 | lg "github.com/Ulbora/Level_Logger" 12 | db "github.com/Ulbora/dbinterface" 13 | mdb "github.com/Ulbora/dbinterface_mysql" 14 | ) 15 | 16 | var dbIgi db.Database 17 | var odbIgi odb.Oauth2DB 18 | var cidIgi int64 19 | var spIDIgi int64 20 | var spID2Igi int64 21 | 22 | func TestMySQLOauthDBIgi_Connect(t *testing.T) { 23 | //var db db.Database 24 | var mydb mdb.MyDB 25 | mydb.Host = "localhost:3306" 26 | mydb.User = "admin" 27 | mydb.Password = "admin" 28 | mydb.Database = "ulbora_oauth2_server" 29 | dbIgi = &mydb 30 | 31 | var moadb MySQLOauthDB 32 | var l lg.Logger 33 | moadb.Log = &l 34 | moadb.DB = dbIgi 35 | 36 | odbIgi = &moadb 37 | 38 | dbIgi.Connect() 39 | } 40 | 41 | func TestMySQLOauthDBIgi_AddClientNullUri(t *testing.T) { 42 | var c odb.Client 43 | c.Secret = "12345" 44 | c.Name = "tester" 45 | c.Email = "bob@bob.com" 46 | c.WebSite = "www.bob.com" 47 | c.Enabled = true 48 | c.Paid = false 49 | 50 | fmt.Println("before db add") 51 | res, id := odbIgi.AddClient(&c, nil) 52 | fmt.Println("client add res: ", res) 53 | fmt.Println("client id: ", id) 54 | if !res || id == 0 { 55 | t.Fail() 56 | } else { 57 | cidIgi = id 58 | } 59 | } 60 | 61 | func TestMySQLOauthDBIgi_AddImplicitGrantNoScope(t *testing.T) { 62 | 63 | var at odb.AccessToken 64 | at.Token = "someacctoken" 65 | at.Expires = time.Now() 66 | 67 | var ig odb.ImplicitGrant 68 | ig.ClientID = cidIgi 69 | ig.UserID = "1234" 70 | res, igid := odbIgi.AddImplicitGrant(&ig, &at, nil) 71 | if !res || igid <= 0 { 72 | t.Fail() 73 | } 74 | } 75 | 76 | func TestMySQLOauthDBIgi_AddImplicitGrant(t *testing.T) { 77 | 78 | var at odb.AccessToken 79 | at.Token = "someacctoken" 80 | at.Expires = time.Now() 81 | 82 | var ig odb.ImplicitGrant 83 | ig.ClientID = cidIgi 84 | ig.UserID = "1234" 85 | var scope = []string{"test1", "test2"} 86 | res, igid := odbIgi.AddImplicitGrant(&ig, &at, &scope) 87 | if !res || igid <= 0 { 88 | t.Fail() 89 | } 90 | } 91 | 92 | func TestMySQLOauthDBIgi_GetImplicitGrant(t *testing.T) { 93 | res := odbIgi.GetImplicitGrant(cidIgi, "1234") 94 | if len(*res) < 2 { 95 | t.Fail() 96 | } 97 | } 98 | 99 | func TestMySQLOauthDBIgi_GetImplicitGrantByScope(t *testing.T) { 100 | res := odbIgi.GetImplicitGrantByScope(cidIgi, "1234", "test1") 101 | if len(*res) != 1 { 102 | t.Fail() 103 | } 104 | } 105 | 106 | func TestMySQLOauthDBIgi_DeleteImplicitGrant(t *testing.T) { 107 | res := odbIgi.DeleteImplicitGrant(cidIgi, "1234") 108 | if !res { 109 | t.Fail() 110 | } 111 | 112 | } 113 | 114 | func TestMySQLOauthDBIgi_DeleteClient(t *testing.T) { 115 | suc := odbIgi.DeleteClient(cidIgi) 116 | if !suc { 117 | t.Fail() 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /mysqldb/keys.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | //GetAccessTokenKey GetAccessTokenKey 24 | func (d *MySQLOauthDB) GetAccessTokenKey() string { 25 | var rtn string 26 | if !d.testConnection() { 27 | d.DB.Connect() 28 | } 29 | var a []interface{} 30 | row := d.DB.Get(getAccessTokenKey, a...) 31 | if len(row.Row) > 0 { 32 | rtn = row.Row[1] 33 | } 34 | return rtn 35 | } 36 | 37 | //GetRefreshTokenKey GetRefreshTokenKey 38 | func (d *MySQLOauthDB) GetRefreshTokenKey() string { 39 | var rtn string 40 | if !d.testConnection() { 41 | d.DB.Connect() 42 | } 43 | var a []interface{} 44 | row := d.DB.Get(getRefreshTokenKey, a...) 45 | if len(row.Row) > 0 { 46 | rtn = row.Row[1] 47 | } 48 | return rtn 49 | } 50 | 51 | //GetSessionKey GetSessionKey 52 | func (d *MySQLOauthDB) GetSessionKey() string { 53 | var rtn string 54 | if !d.testConnection() { 55 | d.DB.Connect() 56 | } 57 | var a []interface{} 58 | row := d.DB.Get(getSessionKey, a...) 59 | if len(row.Row) > 0 { 60 | rtn = row.Row[1] 61 | } 62 | return rtn 63 | } 64 | -------------------------------------------------------------------------------- /mysqldb/keys_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbKeyi db.Database 16 | var odbKeyi odb.Oauth2DB 17 | var cidKeyi int64 18 | var idKeyi int64 19 | 20 | func TestMySQLOauthDBKeyi_ConnectAllowURI(t *testing.T) { 21 | var mydb mdb.MyDB 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbKeyi = &mydb 27 | 28 | var moadb MySQLOauthDB 29 | var l lg.Logger 30 | moadb.Log = &l 31 | moadb.DB = dbKeyi 32 | 33 | odbKeyi = &moadb 34 | 35 | dbKeyi.Connect() 36 | } 37 | 38 | func TestMySQLOauthDBKeyi_GetAccessTokenKey(t *testing.T) { 39 | key := odbKeyi.GetAccessTokenKey() 40 | fmt.Println("access token key: ", key) 41 | if key == "" { 42 | t.Fail() 43 | } 44 | } 45 | 46 | func TestMySQLOauthDBKeyi_GetRefreshTokenKey(t *testing.T) { 47 | key := odbKeyi.GetRefreshTokenKey() 48 | fmt.Println("refresh token key: ", key) 49 | if key == "" { 50 | t.Fail() 51 | } 52 | } 53 | 54 | func TestMySQLOauthDBKeyi_GetSessionKey(t *testing.T) { 55 | key := odbKeyi.GetSessionKey() 56 | fmt.Println("session key: ", key) 57 | if key == "" { 58 | t.Fail() 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /mysqldb/keys_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbKey db.Database 14 | var odbKey odb.Oauth2DB 15 | var cidKey int64 16 | var idKey int64 17 | 18 | func TestMySQLOauthDBKey_ConnectAllowURI(t *testing.T) { 19 | var mydb mdb.MyDBMock 20 | mydb.Host = "localhost:3306" 21 | mydb.User = "admin" 22 | mydb.Password = "admin" 23 | mydb.Database = "ulbora_oauth2_server" 24 | dbKey = &mydb 25 | 26 | var moadb MySQLOauthDB 27 | var l lg.Logger 28 | moadb.Log = &l 29 | moadb.DB = dbKey 30 | 31 | odbKey = &moadb 32 | 33 | dbKey.Connect() 34 | } 35 | 36 | func TestMySQLOauthDBKey_GetAccessTokenKey(t *testing.T) { 37 | var mydb mdb.MyDBMock 38 | mydb.Host = "localhost:3306" 39 | mydb.User = "admin" 40 | mydb.Password = "admin" 41 | mydb.Database = "ulbora_oauth2_server" 42 | dbKey = &mydb 43 | 44 | var mTestRow db.DbRow 45 | mTestRow.Row = []string{} 46 | mydb.MockTestRow = &mTestRow 47 | 48 | var getRow db.DbRow 49 | getRow.Row = []string{"1", "somekey"} 50 | mydb.MockRow1 = &getRow 51 | 52 | var moadb MySQLOauthDB 53 | var l lg.Logger 54 | moadb.Log = &l 55 | moadb.DB = dbKey 56 | 57 | odbKey = &moadb 58 | 59 | dbKey.Connect() 60 | 61 | key := odbKey.GetAccessTokenKey() 62 | fmt.Println("access token key: ", key) 63 | if key == "" { 64 | t.Fail() 65 | } 66 | } 67 | 68 | func TestMySQLOauthDBKey_GetRefreshTokenKey(t *testing.T) { 69 | var mydb mdb.MyDBMock 70 | mydb.Host = "localhost:3306" 71 | mydb.User = "admin" 72 | mydb.Password = "admin" 73 | mydb.Database = "ulbora_oauth2_server" 74 | dbKey = &mydb 75 | 76 | var mTestRow db.DbRow 77 | mTestRow.Row = []string{} 78 | mydb.MockTestRow = &mTestRow 79 | 80 | var getRow db.DbRow 81 | getRow.Row = []string{"1", "somekey"} 82 | mydb.MockRow1 = &getRow 83 | 84 | var moadb MySQLOauthDB 85 | var l lg.Logger 86 | moadb.Log = &l 87 | moadb.DB = dbKey 88 | 89 | odbKey = &moadb 90 | 91 | dbKey.Connect() 92 | 93 | key := odbKey.GetRefreshTokenKey() 94 | fmt.Println("refresh token key: ", key) 95 | if key == "" { 96 | t.Fail() 97 | } 98 | } 99 | 100 | func TestMySQLOauthDBKey_GetSessionKey(t *testing.T) { 101 | var mydb mdb.MyDBMock 102 | mydb.Host = "localhost:3306" 103 | mydb.User = "admin" 104 | mydb.Password = "admin" 105 | mydb.Database = "ulbora_oauth2_server" 106 | dbKey = &mydb 107 | 108 | var mTestRow db.DbRow 109 | mTestRow.Row = []string{} 110 | mydb.MockTestRow = &mTestRow 111 | 112 | var getRow db.DbRow 113 | getRow.Row = []string{"1", "somekey"} 114 | mydb.MockRow1 = &getRow 115 | 116 | var moadb MySQLOauthDB 117 | var l lg.Logger 118 | moadb.Log = &l 119 | moadb.DB = dbKey 120 | 121 | odbKey = &moadb 122 | 123 | dbKey.Connect() 124 | 125 | key := odbKey.GetSessionKey() 126 | fmt.Println("session key: ", key) 127 | if key == "" { 128 | t.Fail() 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /mysqldb/oauthDb.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | import ( 23 | "fmt" 24 | "strconv" 25 | 26 | lg "github.com/Ulbora/Level_Logger" 27 | dbi "github.com/Ulbora/dbinterface" 28 | ) 29 | 30 | //MySQLOauthDB MySQLOauthDB 31 | type MySQLOauthDB struct { 32 | DB dbi.Database 33 | Log *lg.Logger 34 | } 35 | 36 | func (d *MySQLOauthDB) testConnection() bool { 37 | d.Log.Debug("in testConnection") 38 | var rtn = false 39 | var a []interface{} 40 | d.Log.Debug("d.DB: ", fmt.Sprintln(d.DB)) 41 | rowPtr := d.DB.Test(oauthTest, a...) 42 | d.Log.Debug("rowPtr", *rowPtr) 43 | d.Log.Debug("after testConnection test", *rowPtr) 44 | if len(rowPtr.Row) != 0 { 45 | foundRow := rowPtr.Row 46 | int64Val, err := strconv.ParseInt(foundRow[0], 10, 0) 47 | //log.Print("Records found during test ") 48 | //log.Println("Records found during test :", int64Val) 49 | if err != nil { 50 | d.Log.Error(err) 51 | } 52 | if int64Val >= 0 { 53 | rtn = true 54 | } 55 | } 56 | return rtn 57 | } 58 | -------------------------------------------------------------------------------- /mysqldb/passwordGrant_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | odb "github.com/Ulbora/GoAuth2/oauth2database" 11 | lg "github.com/Ulbora/Level_Logger" 12 | db "github.com/Ulbora/dbinterface" 13 | mdb "github.com/Ulbora/dbinterface_mysql" 14 | ) 15 | 16 | var dbPgi db.Database 17 | var odbPgi odb.Oauth2DB 18 | var cidPgi int64 19 | var spIDPgi int64 20 | var spID2Pgi int64 21 | 22 | func TestMySQLOauthDBPgi_Connect(t *testing.T) { 23 | //var db db.Database 24 | var mydb mdb.MyDB 25 | mydb.Host = "localhost:3306" 26 | mydb.User = "admin" 27 | mydb.Password = "admin" 28 | mydb.Database = "ulbora_oauth2_server" 29 | dbPgi = &mydb 30 | 31 | var moadb MySQLOauthDB 32 | var l lg.Logger 33 | moadb.Log = &l 34 | moadb.DB = dbPgi 35 | 36 | odbPgi = &moadb 37 | 38 | dbPgi.Connect() 39 | } 40 | 41 | func TestMySQLOauthDBPgi_AddClientNullUri(t *testing.T) { 42 | var c odb.Client 43 | c.Secret = "12345" 44 | c.Name = "tester" 45 | c.Email = "bob@bob.com" 46 | c.WebSite = "www.bob.com" 47 | c.Enabled = true 48 | c.Paid = false 49 | 50 | fmt.Println("before db add") 51 | res, id := odbPgi.AddClient(&c, nil) 52 | fmt.Println("client add res: ", res) 53 | fmt.Println("client id: ", id) 54 | if !res || id == 0 { 55 | t.Fail() 56 | } else { 57 | cidPgi = id 58 | } 59 | } 60 | 61 | func TestMySQLOauthDBPgi_AddPasswordGrant(t *testing.T) { 62 | 63 | var rt odb.RefreshToken 64 | rt.Token = "somereftoken2" 65 | 66 | var at odb.AccessToken 67 | at.Token = "someacctoken" 68 | at.Expires = time.Now() 69 | 70 | var pwg odb.PasswordGrant 71 | pwg.ClientID = cidPgi 72 | pwg.UserID = "1234" 73 | res, id := odbPgi.AddPasswordGrant(&pwg, &at, &rt) 74 | if !res || id < 1 { 75 | t.Fail() 76 | } 77 | } 78 | 79 | func TestMySQLOauthDBPgi_DeletePasswordGrant(t *testing.T) { 80 | res := odbPgi.DeletePasswordGrant(cidPgi, "1234") 81 | if !res { 82 | t.Fail() 83 | } 84 | } 85 | 86 | func TestMySQLOauthDBPgi_DeleteClient(t *testing.T) { 87 | suc := odbPgi.DeleteClient(cidPgi) 88 | if !suc { 89 | t.Fail() 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /mysqldb/redirectUri.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | import ( 23 | "strconv" 24 | 25 | odb "github.com/Ulbora/GoAuth2/oauth2database" 26 | dbtx "github.com/Ulbora/dbinterface" 27 | ) 28 | 29 | //AddClientRedirectURI AddClientRedirectURI 30 | func (d *MySQLOauthDB) AddClientRedirectURI(tx dbtx.Transaction, ru *odb.ClientRedirectURI) (bool, int64) { 31 | var suc bool 32 | var id int64 33 | if tx == nil && !d.testConnection() { 34 | d.DB.Connect() 35 | } 36 | var a []interface{} 37 | a = append(a, ru.URI, ru.ClientID) 38 | if tx == nil { 39 | suc, id = d.DB.Insert(insertRedirectURI, a...) 40 | } else { 41 | suc, id = tx.Insert(insertRedirectURI, a...) 42 | } 43 | 44 | return suc, id 45 | } 46 | 47 | //GetClientRedirectURIList GetClientRedirectURIList 48 | func (d *MySQLOauthDB) GetClientRedirectURIList(clientID int64) *[]odb.ClientRedirectURI { 49 | if !d.testConnection() { 50 | d.DB.Connect() 51 | } 52 | var rtn []odb.ClientRedirectURI 53 | var a []interface{} 54 | a = append(a, clientID) 55 | rows := d.DB.GetList(getRedirectURIList, a...) 56 | if rows != nil && len(rows.Rows) != 0 { 57 | foundRows := rows.Rows 58 | for r := range foundRows { 59 | foundRow := foundRows[r] 60 | rowContent := parseClientURIRow(&foundRow) 61 | rtn = append(rtn, *rowContent) 62 | } 63 | } 64 | return &rtn 65 | } 66 | 67 | //GetClientRedirectURI GetClientRedirectURI 68 | func (d *MySQLOauthDB) GetClientRedirectURI(clientID int64, uri string) *odb.ClientRedirectURI { 69 | if !d.testConnection() { 70 | d.DB.Connect() 71 | } 72 | var a []interface{} 73 | a = append(a, clientID, uri) 74 | row := d.DB.Get(getRedirectURI, a...) 75 | rtn := parseClientURIRow(&row.Row) 76 | return rtn 77 | } 78 | 79 | //DeleteClientRedirectURI DeleteClientRedirectURI 80 | func (d *MySQLOauthDB) DeleteClientRedirectURI(tx dbtx.Transaction, id int64) bool { 81 | var suc bool 82 | if tx == nil && !d.testConnection() { 83 | d.DB.Connect() 84 | } 85 | var a []interface{} 86 | a = append(a, id) 87 | if tx == nil { 88 | suc = d.DB.Delete(deleteRedirectURI, a...) 89 | } else { 90 | suc = tx.Delete(deleteRedirectURI, a...) 91 | } 92 | return suc 93 | } 94 | 95 | //DeleteClientAllRedirectURI DeleteClientAllRedirectURI 96 | func (d *MySQLOauthDB) DeleteClientAllRedirectURI(tx dbtx.Transaction, clientID int64) bool { 97 | var suc bool 98 | if tx == nil && !d.testConnection() { 99 | d.DB.Connect() 100 | } 101 | var a []interface{} 102 | a = append(a, clientID) 103 | if tx == nil { 104 | suc = d.DB.Delete(deleteAllRedirectURI, a...) 105 | } else { 106 | suc = tx.Delete(deleteAllRedirectURI, a...) 107 | } 108 | return suc 109 | } 110 | 111 | func parseClientURIRow(foundRow *[]string) *odb.ClientRedirectURI { 112 | var rtn odb.ClientRedirectURI 113 | if len(*foundRow) > 0 { 114 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 115 | if err == nil { 116 | clientID, err := strconv.ParseInt((*foundRow)[2], 10, 64) 117 | if err == nil { 118 | rtn.ID = id 119 | rtn.ClientID = clientID 120 | rtn.URI = (*foundRow)[1] 121 | } 122 | } 123 | } 124 | return &rtn 125 | } 126 | -------------------------------------------------------------------------------- /mysqldb/redirectUri_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbbUri db.Database 16 | var odbbUri odb.Oauth2DB 17 | var rdidi int64 18 | var cidUri int64 19 | 20 | func TestMySQLOauthDB_Connect(t *testing.T) { 21 | 22 | //var db db.Database 23 | var mydb mdb.MyDB 24 | mydb.Host = "localhost:3306" 25 | mydb.User = "admin" 26 | mydb.Password = "admin" 27 | mydb.Database = "ulbora_oauth2_server" 28 | dbbUri = &mydb 29 | 30 | var moadb MySQLOauthDB 31 | var l lg.Logger 32 | moadb.Log = &l 33 | moadb.DB = dbbUri 34 | 35 | odbbUri = &moadb 36 | 37 | dbbUri.Connect() 38 | 39 | } 40 | 41 | func TestMySQLOauthDB_AddClientNullUri(t *testing.T) { 42 | var c odb.Client 43 | c.Secret = "12345" 44 | c.Name = "tester" 45 | c.Email = "bob@bob.com" 46 | c.WebSite = "www.bob.com" 47 | c.Enabled = true 48 | c.Paid = false 49 | 50 | fmt.Println("before db add") 51 | res, id := odbbUri.AddClient(&c, nil) 52 | fmt.Println("res: ", res) 53 | fmt.Println("id: ", id) 54 | if !res || id == 0 { 55 | t.Fail() 56 | } else { 57 | cidUri = id 58 | } 59 | } 60 | 61 | func TestMySQLOauthDB_AddClientRedirectURI(t *testing.T) { 62 | var ur odb.ClientRedirectURI 63 | ur.ClientID = cidUri 64 | ur.URI = "someuri" 65 | res, id := odbbUri.AddClientRedirectURI(nil, &ur) 66 | if !res || id <= 0 { 67 | t.Fail() 68 | } else { 69 | rdidi = id 70 | } 71 | } 72 | 73 | func TestMySQLOauthDB_GetClientRedirectURIList(t *testing.T) { 74 | res := odbbUri.GetClientRedirectURIList(cidUri) 75 | fmt.Println("uri res: ", res) 76 | if res == nil || (*res)[0].ClientID != cidUri { 77 | t.Fail() 78 | } 79 | } 80 | 81 | func TestMySQLOauthDB_GetClientRedirectURI(t *testing.T) { 82 | res := odbbUri.GetClientRedirectURI(cidUri, "someuri") 83 | fmt.Println("uri res by id: ", res) 84 | if res == nil || (*res).ClientID != cidUri { 85 | t.Fail() 86 | } 87 | } 88 | 89 | func TestMySQLOauthDB_DeleteClientRedirectURI(t *testing.T) { 90 | res := odbbUri.DeleteClientRedirectURI(nil, rdidi) 91 | fmt.Println("uri delete: ", res) 92 | if !res { 93 | t.Fail() 94 | } 95 | } 96 | 97 | func TestMySQLOauthDB_DeleteClient(t *testing.T) { 98 | suc := odbbUri.DeleteClient(cidUri) 99 | if !suc { 100 | t.Fail() 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /mysqldb/redirectUri_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | //"fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbbUri2 db.Database 14 | var odbbUri2 odb.Oauth2DB 15 | var rdid2 int64 16 | var cidUri2 int64 17 | 18 | func TestMySQLOauthDB2_Connect(t *testing.T) { 19 | 20 | //var db db.Database 21 | var mydb mdb.MyDBMock 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbbUri2 = &mydb 27 | 28 | //mydb.MockTestRow 29 | var mTestRow db.DbRow 30 | mTestRow.Row = []string{} 31 | mydb.MockTestRow = &mTestRow 32 | mydb.MockInsertSuccess1 = true 33 | mydb.MockInsertID1 = 1 34 | 35 | var getRow db.DbRow 36 | getRow.Row = []string{"1", "test", "2"} 37 | mydb.MockRow1 = &getRow 38 | 39 | var rows [][]string 40 | row1 := []string{"1", "tester5", "2"} 41 | rows = append(rows, row1) 42 | var dbrows db.DbRows 43 | dbrows.Rows = rows 44 | mydb.MockRows1 = &dbrows 45 | mydb.MockDeleteSuccess1 = true 46 | mydb.MockDeleteSuccess2 = true 47 | 48 | var moadb MySQLOauthDB 49 | var l lg.Logger 50 | moadb.Log = &l 51 | moadb.DB = dbbUri2 52 | 53 | odbbUri2 = &moadb 54 | 55 | dbbUri2.Connect() 56 | 57 | } 58 | 59 | func TestMySQLOauthDB2_AddClientRedirectURI(t *testing.T) { 60 | var ur odb.ClientRedirectURI 61 | ur.ClientID = 4 62 | ur.URI = "someuri" 63 | res, id := odbbUri2.AddClientRedirectURI(nil, &ur) 64 | if !res || id <= 0 { 65 | t.Fail() 66 | } 67 | } 68 | 69 | func TestMySQLOauthDB2_AddClientRedirectURITx(t *testing.T) { 70 | var ur odb.ClientRedirectURI 71 | ur.ClientID = 4 72 | ur.URI = "someuri" 73 | 74 | var mtx mdb.MyDbTxMock 75 | var mdbx mdb.MyDBMock 76 | mdbx.MockInsertSuccess1 = true 77 | mdbx.MockInsertID1 = 1 78 | mtx.MyDBMock = &mdbx 79 | var moadbtx MySQLOauthDB 80 | var l lg.Logger 81 | moadbtx.Log = &l 82 | //moadbtx.Tx = &mtx 83 | var odbbUri2TX = &moadbtx 84 | 85 | res, id := odbbUri2TX.AddClientRedirectURI(&mtx, &ur) 86 | if !res || id <= 0 { 87 | t.Fail() 88 | } 89 | } 90 | 91 | func TestMySQLOauthDB2_GetClientRedirectURI(t *testing.T) { 92 | var cid int64 = 2 93 | res := odbbUri2.GetClientRedirectURI(cid, "someuri") 94 | if res == nil { 95 | t.Fail() 96 | } 97 | } 98 | 99 | func TestMySQLOauthDB2_GetClientRedirectURIList(t *testing.T) { 100 | var cid int64 = 2 101 | res := odbbUri2.GetClientRedirectURIList(cid) 102 | if res == nil || len(*res) == 0 { 103 | t.Fail() 104 | } 105 | } 106 | 107 | func TestMySQLOauthDB2_DeleteClientRedirectURI(t *testing.T) { 108 | var id int64 = 2 109 | res := odbbUri2.DeleteClientRedirectURI(nil, id) 110 | if !res { 111 | t.Fail() 112 | } 113 | } 114 | 115 | func TestMySQLOauthDB2_DeleteClientRedirectURIAll(t *testing.T) { 116 | var id int64 = 2 117 | res := odbbUri2.DeleteClientAllRedirectURI(nil, id) 118 | if !res { 119 | t.Fail() 120 | } 121 | } 122 | 123 | func TestMySQLOauthDB2_DeleteClientRedirectURITx(t *testing.T) { 124 | var id int64 = 2 125 | 126 | var mtx mdb.MyDbTxMock 127 | var mdbx mdb.MyDBMock 128 | mdbx.MockDeleteSuccess1 = true 129 | mtx.MyDBMock = &mdbx 130 | var moadbtx MySQLOauthDB 131 | var l lg.Logger 132 | moadbtx.Log = &l 133 | //moadbtx.Tx = &mtx 134 | var odbbUri2TX = &moadbtx 135 | 136 | res := odbbUri2TX.DeleteClientRedirectURI(&mtx, id) 137 | if !res { 138 | t.Fail() 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /mysqldb/refreshToken.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | dbtx "github.com/Ulbora/dbinterface" 28 | ) 29 | 30 | //AddRefreshToken AddRefreshToken 31 | func (d *MySQLOauthDB) AddRefreshToken(tx dbtx.Transaction, t *odb.RefreshToken) (bool, int64) { 32 | var suc bool 33 | var id int64 34 | if tx == nil && !d.testConnection() { 35 | d.DB.Connect() 36 | } 37 | var a []interface{} 38 | a = append(a, t.Token) 39 | if tx == nil { 40 | suc, id = d.DB.Insert(insertRefreshToken, a...) 41 | } else { 42 | suc, id = tx.Insert(insertRefreshToken, a...) 43 | } 44 | return suc, id 45 | } 46 | 47 | //UpdateRefreshToken UpdateRefreshToken 48 | func (d *MySQLOauthDB) UpdateRefreshToken(t *odb.RefreshToken) bool { 49 | if !d.testConnection() { 50 | d.DB.Connect() 51 | } 52 | var a []interface{} 53 | a = append(a, t.Token, t.ID) 54 | suc := d.DB.Update(updateRefreshToken, a...) 55 | return suc 56 | } 57 | 58 | //GetRefreshToken GetRefreshToken 59 | func (d *MySQLOauthDB) GetRefreshToken(id int64) *odb.RefreshToken { 60 | if !d.testConnection() { 61 | d.DB.Connect() 62 | } 63 | var a []interface{} 64 | a = append(a, id) 65 | row := d.DB.Get(getRefreshToken, a...) 66 | rtn := parseRefreshTokenRow(&row.Row) 67 | return rtn 68 | } 69 | 70 | //DeleteRefreshToken DeleteRefreshToken 71 | func (d *MySQLOauthDB) DeleteRefreshToken(tx dbtx.Transaction, id int64) bool { 72 | var suc bool 73 | if tx == nil && !d.testConnection() { 74 | d.DB.Connect() 75 | } 76 | var a []interface{} 77 | a = append(a, id) 78 | if tx == nil { 79 | suc = d.DB.Delete(deleteRefreshToken, a...) 80 | } else { 81 | suc = tx.Delete(deleteRefreshToken, a...) 82 | } 83 | return suc 84 | } 85 | 86 | func parseRefreshTokenRow(foundRow *[]string) *odb.RefreshToken { 87 | var rtn odb.RefreshToken 88 | if len(*foundRow) > 0 { 89 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 90 | if err == nil { 91 | rtn.ID = id 92 | rtn.Token = (*foundRow)[1] 93 | } 94 | } 95 | return &rtn 96 | } 97 | -------------------------------------------------------------------------------- /mysqldb/refreshToken_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbRti db.Database 16 | var odbRti odb.Oauth2DB 17 | var idRti int64 18 | 19 | //var cidRti int64 20 | 21 | func TestMySQLOauthDBReTokeni_Connect(t *testing.T) { 22 | //var db db.Database 23 | var mydb mdb.MyDB 24 | mydb.Host = "localhost:3306" 25 | mydb.User = "admin" 26 | mydb.Password = "admin" 27 | mydb.Database = "ulbora_oauth2_server" 28 | dbRti = &mydb 29 | 30 | var moadb MySQLOauthDB 31 | var l lg.Logger 32 | moadb.Log = &l 33 | moadb.DB = dbRti 34 | 35 | odbRti = &moadb 36 | 37 | dbRti.Connect() 38 | } 39 | 40 | func TestMySQLOauthDBReTokeni_AddRefreshToken(t *testing.T) { 41 | var tk odb.RefreshToken 42 | tk.Token = "somereftoken" 43 | res, id := odbRti.AddRefreshToken(nil, &tk) 44 | if !res || id <= 0 { 45 | t.Fail() 46 | } else { 47 | idRti = id 48 | } 49 | } 50 | 51 | func TestMySQLOauthDBReTokeni_UpdateRefreshToken(t *testing.T) { 52 | var tk odb.RefreshToken 53 | tk.ID = idRti 54 | tk.Token = "somereftoken2" 55 | res := odbRti.UpdateRefreshToken(&tk) 56 | if !res { 57 | t.Fail() 58 | } 59 | } 60 | 61 | func TestMySQLOauthDBReTokeni_GetRefreshToken(t *testing.T) { 62 | res := odbRti.GetRefreshToken(idRti) 63 | fmt.Println("ref token: ", res) 64 | if res == nil || (*res).Token != "somereftoken2" { 65 | t.Fail() 66 | } 67 | } 68 | 69 | func TestMySQLOauthDBReTokeni_DeleteRefreshToken(t *testing.T) { 70 | res := odbRti.DeleteRefreshToken(nil, idRti) 71 | fmt.Println("del ref token: ", res) 72 | if !res { 73 | t.Fail() 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /mysqldb/refreshToken_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbRt db.Database 14 | var odbRt odb.Oauth2DB 15 | var idRt int64 16 | 17 | //var cidRti int64 18 | 19 | func TestMySQLOauthDBReToken_Connect(t *testing.T) { 20 | //var db db.Database 21 | var mydb mdb.MyDBMock 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbRt = &mydb 27 | 28 | var mTestRow db.DbRow 29 | mTestRow.Row = []string{} 30 | mydb.MockTestRow = &mTestRow 31 | 32 | mydb.MockInsertSuccess1 = true 33 | mydb.MockInsertID1 = 1 34 | 35 | var getRow db.DbRow 36 | getRow.Row = []string{"1", "somereftoken2"} 37 | mydb.MockRow1 = &getRow 38 | 39 | mydb.MockUpdateSuccess1 = true 40 | 41 | mydb.MockDeleteSuccess1 = true 42 | 43 | var moadb MySQLOauthDB 44 | var l lg.Logger 45 | moadb.Log = &l 46 | moadb.DB = dbRt 47 | 48 | odbRt = &moadb 49 | 50 | dbRt.Connect() 51 | } 52 | 53 | func TestMySQLOauthDBReToken_AddRefreshToken(t *testing.T) { 54 | var tk odb.RefreshToken 55 | tk.Token = "somereftoken" 56 | res, id := odbRt.AddRefreshToken(nil, &tk) 57 | if !res || id <= 0 { 58 | t.Fail() 59 | } else { 60 | idRt = id 61 | } 62 | } 63 | 64 | func TestMySQLOauthDBReToken_AddRefreshTokenTx(t *testing.T) { 65 | var tk odb.RefreshToken 66 | tk.Token = "somereftoken" 67 | 68 | var mtx mdb.MyDbTxMock 69 | var mdbx mdb.MyDBMock 70 | mdbx.MockInsertSuccess1 = true 71 | mdbx.MockInsertID1 = 1 72 | mtx.MyDBMock = &mdbx 73 | var moadbtx MySQLOauthDB 74 | var l lg.Logger 75 | moadbtx.Log = &l 76 | //moadbtx.Tx = &mtx 77 | var odbbUri2TX = &moadbtx 78 | 79 | res, id := odbbUri2TX.AddRefreshToken(&mtx, &tk) 80 | if !res || id <= 0 { 81 | t.Fail() 82 | } else { 83 | idRt = id 84 | } 85 | } 86 | 87 | func TestMySQLOauthDBReToken_UpdateRefreshToken(t *testing.T) { 88 | var tk odb.RefreshToken 89 | tk.ID = idRt 90 | tk.Token = "somereftoken2" 91 | res := odbRt.UpdateRefreshToken(&tk) 92 | if !res { 93 | t.Fail() 94 | } 95 | } 96 | 97 | func TestMySQLOauthDBReToken_GetRefreshToken(t *testing.T) { 98 | res := odbRt.GetRefreshToken(idRt) 99 | fmt.Println("ref token: ", res) 100 | if res == nil || (*res).Token != "somereftoken2" { 101 | t.Fail() 102 | } 103 | } 104 | 105 | func TestMySQLOauthDBReToken_DeleteRefreshToken(t *testing.T) { 106 | res := odbRt.DeleteRefreshToken(nil, idRt) 107 | fmt.Println("del ref token: ", res) 108 | if !res { 109 | t.Fail() 110 | } 111 | } 112 | 113 | func TestMySQLOauthDBReToken_DeleteRefreshTokenTx(t *testing.T) { 114 | var mtx mdb.MyDbTxMock 115 | var mdbx mdb.MyDBMock 116 | mdbx.MockDeleteSuccess1 = true 117 | mtx.MyDBMock = &mdbx 118 | var moadbtx MySQLOauthDB 119 | var l lg.Logger 120 | moadbtx.Log = &l 121 | //moadbtx.Tx = &mtx 122 | var odbbUri2TX = &moadbtx 123 | res := odbbUri2TX.DeleteRefreshToken(&mtx, idRt) 124 | fmt.Println("del ref token: ", res) 125 | if !res { 126 | t.Fail() 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /mysqldb/roleUri.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //AddClientRoleURI AddClientRoleURI 30 | func (d *MySQLOauthDB) AddClientRoleURI(r *odb.ClientRoleURI) bool { 31 | if !d.testConnection() { 32 | d.DB.Connect() 33 | } 34 | var a []interface{} 35 | a = append(a, r.ClientRoleID, r.ClientAllowedURIID) 36 | suc, _ := d.DB.Insert(insertRoleURI, a...) 37 | return suc 38 | } 39 | 40 | //GetClientRoleAllowedURIList GetClientRoleAllowedURIList 41 | func (d *MySQLOauthDB) GetClientRoleAllowedURIList(roleID int64) *[]odb.ClientRoleURI { 42 | var rtn []odb.ClientRoleURI 43 | if !d.testConnection() { 44 | d.DB.Connect() 45 | } 46 | var a []interface{} 47 | a = append(a, roleID) 48 | rows := d.DB.GetList(getRoleURIList, a...) 49 | if rows != nil && len(rows.Rows) != 0 { 50 | foundRows := rows.Rows 51 | for r := range foundRows { 52 | foundRow := foundRows[r] 53 | rowContent := parseRowURIRow(&foundRow) 54 | rtn = append(rtn, *rowContent) 55 | } 56 | } 57 | return &rtn 58 | } 59 | 60 | // GetClientRoleAllowedURIListByClientID GetClientRoleAllowedURIListByClientID 61 | func (d *MySQLOauthDB) GetClientRoleAllowedURIListByClientID(clientID int64) *[]odb.RoleURI { 62 | var rtn []odb.RoleURI 63 | if !d.testConnection() { 64 | d.DB.Connect() 65 | } 66 | var a []interface{} 67 | a = append(a, clientID) 68 | rows := d.DB.GetList(roleURIJoin, a...) 69 | if rows != nil && len(rows.Rows) != 0 { 70 | foundRows := rows.Rows 71 | for r := range foundRows { 72 | foundRow := foundRows[r] 73 | rowContent := parseClientRowURIRow(&foundRow) 74 | rtn = append(rtn, *rowContent) 75 | } 76 | } 77 | return &rtn 78 | } 79 | 80 | //DeleteClientRoleURI DeleteClientRoleURI 81 | func (d *MySQLOauthDB) DeleteClientRoleURI(r *odb.ClientRoleURI) bool { 82 | if !d.testConnection() { 83 | d.DB.Connect() 84 | } 85 | var a []interface{} 86 | a = append(a, r.ClientRoleID, r.ClientAllowedURIID) 87 | return d.DB.Delete(deleteRoleURI, a...) 88 | } 89 | 90 | func parseRowURIRow(foundRow *[]string) *odb.ClientRoleURI { 91 | var rtn odb.ClientRoleURI 92 | roleID, err := strconv.ParseInt((*foundRow)[0], 10, 64) 93 | if err == nil { 94 | URIID, err := strconv.ParseInt((*foundRow)[1], 10, 64) 95 | if err == nil { 96 | rtn.ClientRoleID = roleID 97 | rtn.ClientAllowedURIID = URIID 98 | 99 | } 100 | } 101 | return &rtn 102 | } 103 | 104 | func parseClientRowURIRow(foundRow *[]string) *odb.RoleURI { 105 | var rtn odb.RoleURI 106 | if len(*foundRow) > 0 { 107 | roleID, err := strconv.ParseInt((*foundRow)[0], 10, 64) 108 | if err == nil { 109 | URIID, err := strconv.ParseInt((*foundRow)[2], 10, 64) 110 | if err == nil { 111 | clientID, err := strconv.ParseInt((*foundRow)[4], 10, 64) 112 | if err == nil { 113 | rtn.ClientRoleID = roleID 114 | rtn.ClientAllowedURIID = URIID 115 | rtn.ClientID = clientID 116 | rtn.Role = (*foundRow)[1] 117 | rtn.ClientAllowedURI = (*foundRow)[3] 118 | } 119 | } 120 | } 121 | } 122 | return &rtn 123 | } 124 | -------------------------------------------------------------------------------- /mysqldb/roleUri_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbroui db.Database 16 | var odbroui odb.Oauth2DB 17 | var cidroui int64 18 | 19 | //uil id 20 | var idrouiUidi int64 21 | 22 | //role id 23 | var idrouiRoidi int64 24 | 25 | var idroui int64 26 | 27 | func TestMySQLOauthDBRoleUrii_ConnectAllowURI(t *testing.T) { 28 | var mydb mdb.MyDB 29 | mydb.Host = "localhost:3306" 30 | mydb.User = "admin" 31 | mydb.Password = "admin" 32 | mydb.Database = "ulbora_oauth2_server" 33 | dbroui = &mydb 34 | 35 | var moadb MySQLOauthDB 36 | var l lg.Logger 37 | moadb.Log = &l 38 | moadb.DB = dbroui 39 | 40 | odbroui = &moadb 41 | 42 | dbroui.Connect() 43 | } 44 | 45 | func TestMySQLOauthDBRoleUrii_AddClient(t *testing.T) { 46 | var c odb.Client 47 | c.Secret = "12345" 48 | c.Name = "tester" 49 | c.Email = "bob@bob.com" 50 | c.WebSite = "www.bob.com" 51 | c.Enabled = true 52 | c.Paid = false 53 | 54 | fmt.Println("before db add") 55 | res, id := odbroui.AddClient(&c, nil) 56 | fmt.Println("res: ", res) 57 | fmt.Println("id: ", id) 58 | if !res || id == 0 { 59 | t.Fail() 60 | } else { 61 | cidroui = id 62 | } 63 | } 64 | 65 | func TestMySQLOauthDBRoleUrii_AddClientAllowedURI(t *testing.T) { 66 | var ur odb.ClientAllowedURI 67 | ur.ClientID = cidroui 68 | ur.URI = "someuri" 69 | res, id := odbroui.AddClientAllowedURI(&ur) 70 | if !res || id <= 0 { 71 | t.Fail() 72 | } else { 73 | idrouiUidi = id 74 | } 75 | } 76 | 77 | func TestMySQLOauthDBRoleUrii_AddClientRole(t *testing.T) { 78 | var r odb.ClientRole 79 | r.ClientID = cidroui 80 | r.Role = "someRole" 81 | res, id := odbroui.AddClientRole(&r) 82 | if !res || id <= 0 { 83 | t.Fail() 84 | } else { 85 | idrouiRoidi = id 86 | } 87 | } 88 | func TestMySQLOauthDBRoleUrii_AddClientRoleURI(t *testing.T) { 89 | var r odb.ClientRoleURI 90 | r.ClientAllowedURIID = idrouiUidi 91 | r.ClientRoleID = idrouiRoidi 92 | res := odbroui.AddClientRoleURI(&r) 93 | // fmt.Println("role uri id: ", id) 94 | if !res { 95 | t.Fail() 96 | } 97 | } 98 | 99 | func TestMySQLOauthDBRoleUrii_GetClientRoleURIList(t *testing.T) { 100 | res := odbroui.GetClientRoleAllowedURIList(idrouiRoidi) 101 | fmt.Println("Role URI list res: ", res) 102 | if res == nil || (*res)[0].ClientRoleID != idrouiRoidi { 103 | t.Fail() 104 | } 105 | } 106 | 107 | func TestMySQLOauthDBRoleUrii_GetClientRoleURIListByClient(t *testing.T) { 108 | res := odbroui.GetClientRoleAllowedURIListByClientID(cidroui) 109 | fmt.Println("Role URI list by client res: ", res) 110 | if res == nil || (*res)[0].ClientRoleID != idrouiRoidi { 111 | t.Fail() 112 | } 113 | } 114 | 115 | func TestMySQLOauthDBRoleUrii_DeleteClientRoleURI(t *testing.T) { 116 | var r odb.ClientRoleURI 117 | r.ClientAllowedURIID = idrouiUidi 118 | r.ClientRoleID = idrouiRoidi 119 | res := odbroui.DeleteClientRoleURI(&r) 120 | if !res { 121 | t.Fail() 122 | } 123 | } 124 | 125 | func TestMySQLOauthDBRoleUrii_DeleteClientAllowedURI(t *testing.T) { 126 | res := odbroui.DeleteClientAllowedURI(idrouiUidi) 127 | fmt.Println("allowed uri delete: ", res) 128 | if !res { 129 | t.Fail() 130 | } 131 | } 132 | 133 | func TestMySQLOauthDBRoleUrii_DeleteClientRole(t *testing.T) { 134 | res := odbroui.DeleteClientRole(idrouiRoidi) 135 | fmt.Println("role delete: ", res) 136 | if !res { 137 | t.Fail() 138 | } 139 | } 140 | 141 | func TestMySQLOauthDBRoleUrii_DeleteClient(t *testing.T) { 142 | suc := odbroui.DeleteClient(cidroui) 143 | if !suc { 144 | t.Fail() 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /mysqldb/roleUri_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbrou db.Database 14 | var odbrou odb.Oauth2DB 15 | var cidrou int64 16 | 17 | //uil id 18 | var idrouiUid int64 19 | 20 | //role id 21 | var idrouiRoid int64 22 | 23 | var idrou int64 24 | 25 | func TestMySQLOauthDBRoleUri_ConnectAllowURI(t *testing.T) { 26 | var mydb mdb.MyDBMock 27 | mydb.Host = "localhost:3306" 28 | mydb.User = "admin" 29 | mydb.Password = "admin" 30 | mydb.Database = "ulbora_oauth2_server" 31 | dbrou = &mydb 32 | 33 | var mTestRow db.DbRow 34 | mTestRow.Row = []string{} 35 | mydb.MockTestRow = &mTestRow 36 | mydb.MockInsertSuccess1 = true 37 | mydb.MockInsertID1 = 1 38 | 39 | var rows [][]string 40 | row1 := []string{"4", "1"} 41 | rows = append(rows, row1) 42 | var dbrows db.DbRows 43 | dbrows.Rows = rows 44 | mydb.MockRows1 = &dbrows 45 | 46 | var rowsb [][]string 47 | row1b := []string{"4", "somerole", "1", "someurl", "2"} 48 | rowsb = append(rowsb, row1b) 49 | var dbrowsb db.DbRows 50 | dbrowsb.Rows = rowsb 51 | mydb.MockRows2 = &dbrowsb 52 | 53 | mydb.MockDeleteSuccess1 = true 54 | 55 | var moadb MySQLOauthDB 56 | var l lg.Logger 57 | moadb.Log = &l 58 | moadb.DB = dbrou 59 | 60 | odbrou = &moadb 61 | 62 | dbrou.Connect() 63 | } 64 | 65 | func TestMySQLOauthDBRoleUri_AddClientRoleURI(t *testing.T) { 66 | cidrou = 2 67 | idrouiUid = 1 68 | idrouiRoid = 4 69 | 70 | var r odb.ClientRoleURI 71 | r.ClientAllowedURIID = idrouiUid 72 | r.ClientRoleID = idrouiRoid 73 | res := odbrou.AddClientRoleURI(&r) 74 | // fmt.Println("role uri id: ", id) 75 | if !res { 76 | t.Fail() 77 | } 78 | } 79 | 80 | func TestMySQLOauthDBRoleUri_GetClientRoleURIList(t *testing.T) { 81 | res := odbrou.GetClientRoleAllowedURIList(idrouiRoid) 82 | fmt.Println("Role URI list res: ", res) 83 | fmt.Println("Role URI list res.ClientRoleID: ", (*res)[0].ClientRoleID) 84 | if res == nil || (*res)[0].ClientRoleID != idrouiRoid { 85 | t.Fail() 86 | } 87 | } 88 | 89 | func TestMySQLOauthDBRoleUri_GetClientRoleURIListByClient(t *testing.T) { 90 | res := odbrou.GetClientRoleAllowedURIListByClientID(cidrou) 91 | fmt.Println("Role URI list by client res: ", res) 92 | if res == nil || (*res)[0].ClientRoleID != idrouiRoid { 93 | t.Fail() 94 | } 95 | } 96 | 97 | func TestMySQLOauthDBRoleUri_DeleteClientRoleURI(t *testing.T) { 98 | var r odb.ClientRoleURI 99 | r.ClientAllowedURIID = idrouiUid 100 | r.ClientRoleID = idrouiRoid 101 | res := odbrou.DeleteClientRoleURI(&r) 102 | if !res { 103 | t.Fail() 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /mysqldb/role_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbRli db.Database 16 | var odbRli odb.Oauth2DB 17 | var cidRli int64 18 | var idRli int64 19 | 20 | func TestMySQLOauthDBi_ConnectRole(t *testing.T) { 21 | var mydb mdb.MyDB 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbRli = &mydb 27 | 28 | var moadb MySQLOauthDB 29 | var l lg.Logger 30 | moadb.Log = &l 31 | moadb.DB = dbRli 32 | 33 | odbRli = &moadb 34 | 35 | dbRli.Connect() 36 | } 37 | 38 | func TestMySQLOauthDBi_AddClientInRole(t *testing.T) { 39 | var c odb.Client 40 | c.Secret = "12345" 41 | c.Name = "tester" 42 | c.Email = "bob@bob.com" 43 | c.WebSite = "www.bob.com" 44 | c.Enabled = true 45 | c.Paid = false 46 | 47 | fmt.Println("before db add") 48 | res, id := odbRli.AddClient(&c, nil) 49 | fmt.Println("res: ", res) 50 | fmt.Println("id: ", id) 51 | if !res || id == 0 { 52 | t.Fail() 53 | } else { 54 | cidRli = id 55 | } 56 | } 57 | 58 | func TestMySQLOauthDBi_AddClientRole(t *testing.T) { 59 | var r odb.ClientRole 60 | r.ClientID = cidRli 61 | r.Role = "someRole" 62 | res, id := odbRli.AddClientRole(&r) 63 | if !res || id <= 0 { 64 | t.Fail() 65 | } else { 66 | idRli = id 67 | } 68 | } 69 | 70 | func TestMySQLOauthDBi_GetClientRoleList(t *testing.T) { 71 | res := odbRli.GetClientRoleList(cidRli) 72 | fmt.Println("Role list res: ", res) 73 | if res == nil || (*res)[0].ClientID != cidRli { 74 | t.Fail() 75 | } 76 | } 77 | 78 | func TestMySQLOauthDBi_DeleteClientRole(t *testing.T) { 79 | res := odbRli.DeleteClientRole(idRli) 80 | fmt.Println("role delete: ", res) 81 | if !res { 82 | t.Fail() 83 | } 84 | } 85 | 86 | func TestMySQLOauthDBi_DeleteClientInRole(t *testing.T) { 87 | suc := odbRli.DeleteClient(cidRli) 88 | if !suc { 89 | t.Fail() 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /mysqldb/role_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbRl db.Database 14 | var odbRl odb.Oauth2DB 15 | var cidRl int64 16 | var idRl int64 17 | 18 | func TestMySQLOauthDB_ConnectRole(t *testing.T) { 19 | var mydb mdb.MyDBMock 20 | mydb.Host = "localhost:3306" 21 | mydb.User = "admin" 22 | mydb.Password = "admin" 23 | mydb.Database = "ulbora_oauth2_server" 24 | dbRl = &mydb 25 | 26 | var mTestRow db.DbRow 27 | mTestRow.Row = []string{} 28 | mydb.MockTestRow = &mTestRow 29 | 30 | mydb.MockInsertSuccess1 = true 31 | mydb.MockInsertID1 = 1 32 | 33 | var rows [][]string 34 | row1 := []string{"1", "tester5", "2"} 35 | rows = append(rows, row1) 36 | var dbrows db.DbRows 37 | dbrows.Rows = rows 38 | mydb.MockRows1 = &dbrows 39 | 40 | mydb.MockDeleteSuccess1 = true 41 | 42 | var moadb MySQLOauthDB 43 | var l lg.Logger 44 | moadb.Log = &l 45 | moadb.DB = dbRl 46 | 47 | odbRl = &moadb 48 | 49 | dbRl.Connect() 50 | } 51 | 52 | func TestMySQLOauthDB_AddClientRole(t *testing.T) { 53 | cidRl = 2 54 | var r odb.ClientRole 55 | r.ClientID = cidRl 56 | r.Role = "someRole" 57 | res, id := odbRl.AddClientRole(&r) 58 | if !res || id <= 0 { 59 | t.Fail() 60 | } else { 61 | idRl = id 62 | } 63 | } 64 | 65 | func TestMySQLOauthDB_GetClientRoleList(t *testing.T) { 66 | res := odbRl.GetClientRoleList(cidRl) 67 | fmt.Println("Role list res: ", res) 68 | if res == nil || (*res)[0].ClientID != cidRl { 69 | t.Fail() 70 | } 71 | } 72 | 73 | func TestMySQLOauthDB_DeleteClientRole(t *testing.T) { 74 | res := odbRl.DeleteClientRole(idRl) 75 | fmt.Println("role delete: ", res) 76 | if !res { 77 | t.Fail() 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /mysqldb/roles.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //AddClientRole AddClientRole 30 | func (d *MySQLOauthDB) AddClientRole(r *odb.ClientRole) (bool, int64) { 31 | if !d.testConnection() { 32 | d.DB.Connect() 33 | } 34 | var a []interface{} 35 | a = append(a, r.Role, r.ClientID) 36 | suc, id := d.DB.Insert(insertRole, a...) 37 | return suc, id 38 | } 39 | 40 | //GetClientRoleList GetClientRoleList 41 | func (d *MySQLOauthDB) GetClientRoleList(clientID int64) *[]odb.ClientRole { 42 | if !d.testConnection() { 43 | d.DB.Connect() 44 | } 45 | var rtn []odb.ClientRole 46 | var a []interface{} 47 | a = append(a, clientID) 48 | rows := d.DB.GetList(getRoleList, a...) 49 | if rows != nil && len(rows.Rows) != 0 { 50 | foundRows := rows.Rows 51 | for r := range foundRows { 52 | foundRow := foundRows[r] 53 | rowContent := parseClientRoleRow(&foundRow) 54 | rtn = append(rtn, *rowContent) 55 | } 56 | } 57 | return &rtn 58 | } 59 | 60 | //DeleteClientRole DeleteClientRole 61 | func (d *MySQLOauthDB) DeleteClientRole(id int64) bool { 62 | if !d.testConnection() { 63 | d.DB.Connect() 64 | } 65 | var a []interface{} 66 | a = append(a, id) 67 | return d.DB.Delete(deleteRole, a...) 68 | } 69 | 70 | func parseClientRoleRow(foundRow *[]string) *odb.ClientRole { 71 | var rtn odb.ClientRole 72 | if len(*foundRow) > 0 { 73 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 74 | if err == nil { 75 | clientID, err := strconv.ParseInt((*foundRow)[2], 10, 64) 76 | if err == nil { 77 | rtn.ID = id 78 | rtn.ClientID = clientID 79 | rtn.Role = (*foundRow)[1] 80 | } 81 | } 82 | } 83 | return &rtn 84 | } 85 | -------------------------------------------------------------------------------- /mysqldb/runinttest.sh: -------------------------------------------------------------------------------- 1 | go test -tags=integration -------------------------------------------------------------------------------- /mysqldb/scope.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | /* 4 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 5 | All rights reserved. 6 | 7 | Copyright (C) 2019 Ken Williamson 8 | All rights reserved. 9 | 10 | This program is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | */ 22 | 23 | import ( 24 | "strconv" 25 | 26 | odb "github.com/Ulbora/GoAuth2/oauth2database" 27 | ) 28 | 29 | //AddClientScope AddClientScope 30 | func (d *MySQLOauthDB) AddClientScope(s *odb.ClientScope) (bool, int64) { 31 | if !d.testConnection() { 32 | d.DB.Connect() 33 | } 34 | var a []interface{} 35 | a = append(a, s.Scope, s.ClientID) 36 | suc, id := d.DB.Insert(insertScope, a...) 37 | return suc, id 38 | } 39 | 40 | //GetClientScopeList GetClientScopeList 41 | func (d *MySQLOauthDB) GetClientScopeList(clientID int64) *[]odb.ClientScope { 42 | if !d.testConnection() { 43 | d.DB.Connect() 44 | } 45 | var rtn []odb.ClientScope 46 | var a []interface{} 47 | a = append(a, clientID) 48 | rows := d.DB.GetList(getScopeList, a...) 49 | if rows != nil && len(rows.Rows) != 0 { 50 | foundRows := rows.Rows 51 | for r := range foundRows { 52 | foundRow := foundRows[r] 53 | rowContent := parseClientScopeRow(&foundRow) 54 | rtn = append(rtn, *rowContent) 55 | } 56 | } 57 | return &rtn 58 | } 59 | 60 | //DeleteClientScope DeleteClientScope 61 | func (d *MySQLOauthDB) DeleteClientScope(id int64) bool { 62 | if !d.testConnection() { 63 | d.DB.Connect() 64 | } 65 | var a []interface{} 66 | a = append(a, id) 67 | return d.DB.Delete(deleteScope, a...) 68 | } 69 | 70 | func parseClientScopeRow(foundRow *[]string) *odb.ClientScope { 71 | var rtn odb.ClientScope 72 | if len(*foundRow) > 0 { 73 | id, err := strconv.ParseInt((*foundRow)[0], 10, 64) 74 | if err == nil { 75 | clientID, err := strconv.ParseInt((*foundRow)[2], 10, 64) 76 | if err == nil { 77 | rtn.ID = id 78 | rtn.ClientID = clientID 79 | rtn.Scope = (*foundRow)[1] 80 | } 81 | } 82 | } 83 | return &rtn 84 | } 85 | -------------------------------------------------------------------------------- /mysqldb/scope_integration_test.go: -------------------------------------------------------------------------------- 1 | // +build integration move to top 2 | 3 | package mysqldb 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | 9 | odb "github.com/Ulbora/GoAuth2/oauth2database" 10 | lg "github.com/Ulbora/Level_Logger" 11 | db "github.com/Ulbora/dbinterface" 12 | mdb "github.com/Ulbora/dbinterface_mysql" 13 | ) 14 | 15 | var dbSoi db.Database 16 | var odbSoi odb.Oauth2DB 17 | var cidSoi int64 18 | var idSoi int64 19 | 20 | func TestMySQLOauthDBi_ConnectScope(t *testing.T) { 21 | var mydb mdb.MyDB 22 | mydb.Host = "localhost:3306" 23 | mydb.User = "admin" 24 | mydb.Password = "admin" 25 | mydb.Database = "ulbora_oauth2_server" 26 | dbSoi = &mydb 27 | 28 | var moadb MySQLOauthDB 29 | var l lg.Logger 30 | moadb.Log = &l 31 | moadb.DB = dbSoi 32 | 33 | odbSoi = &moadb 34 | 35 | dbSoi.Connect() 36 | } 37 | 38 | func TestMySQLOauthDBi_AddClientInScope(t *testing.T) { 39 | var c odb.Client 40 | c.Secret = "12345" 41 | c.Name = "tester" 42 | c.Email = "bob@bob.com" 43 | c.WebSite = "www.bob.com" 44 | c.Enabled = true 45 | c.Paid = false 46 | 47 | fmt.Println("before db add") 48 | res, id := odbSoi.AddClient(&c, nil) 49 | fmt.Println("res: ", res) 50 | fmt.Println("id: ", id) 51 | if !res || id == 0 { 52 | t.Fail() 53 | } else { 54 | cidSoi = id 55 | } 56 | } 57 | 58 | func TestMySQLOauthDBi_AddClientScope(t *testing.T) { 59 | var ur odb.ClientScope 60 | ur.ClientID = cidSoi 61 | ur.Scope = "somescope" 62 | res, id := odbSoi.AddClientScope(&ur) 63 | if !res || id <= 0 { 64 | t.Fail() 65 | } else { 66 | idSoi = id 67 | } 68 | } 69 | 70 | func TestMySQLOauthDBi_GetClientScopeList(t *testing.T) { 71 | res := odbSoi.GetClientScopeList(cidSoi) 72 | fmt.Println("scope list res: ", res) 73 | if res == nil || (*res)[0].ClientID != cidSoi { 74 | t.Fail() 75 | } 76 | } 77 | 78 | func TestMySQLOauthDBi_DeleteClientScope(t *testing.T) { 79 | res := odbSoi.DeleteClientScope(idSoi) 80 | fmt.Println("scope delete: ", res) 81 | if !res { 82 | t.Fail() 83 | } 84 | } 85 | 86 | func TestMySQLOauthDBi_DeleteClientInScope(t *testing.T) { 87 | suc := odbSoi.DeleteClient(cidSoi) 88 | if !suc { 89 | t.Fail() 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /mysqldb/scope_test.go: -------------------------------------------------------------------------------- 1 | package mysqldb 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | odb "github.com/Ulbora/GoAuth2/oauth2database" 8 | lg "github.com/Ulbora/Level_Logger" 9 | db "github.com/Ulbora/dbinterface" 10 | mdb "github.com/Ulbora/dbinterface_mysql" 11 | ) 12 | 13 | var dbSo db.Database 14 | var odbSo odb.Oauth2DB 15 | var cidSo int64 16 | var idSo int64 17 | 18 | func TestMySQLOauthDB_ConnectScope(t *testing.T) { 19 | var mydb mdb.MyDBMock 20 | mydb.Host = "localhost:3306" 21 | mydb.User = "admin" 22 | mydb.Password = "admin" 23 | mydb.Database = "ulbora_oauth2_server" 24 | dbSo = &mydb 25 | 26 | var mTestRow db.DbRow 27 | mTestRow.Row = []string{} 28 | mydb.MockTestRow = &mTestRow 29 | 30 | mydb.MockInsertSuccess1 = true 31 | mydb.MockInsertID1 = 1 32 | 33 | var rows [][]string 34 | row1 := []string{"1", "somescope", "2"} 35 | rows = append(rows, row1) 36 | var dbrows db.DbRows 37 | dbrows.Rows = rows 38 | mydb.MockRows1 = &dbrows 39 | 40 | mydb.MockDeleteSuccess1 = true 41 | 42 | var moadb MySQLOauthDB 43 | var l lg.Logger 44 | moadb.Log = &l 45 | moadb.DB = dbSo 46 | 47 | odbSo = &moadb 48 | 49 | dbSo.Connect() 50 | } 51 | 52 | func TestMySQLOauthDB_AddClientScope(t *testing.T) { 53 | cidSo = 2 54 | var ur odb.ClientScope 55 | ur.ClientID = cidSo 56 | ur.Scope = "somescope" 57 | res, id := odbSo.AddClientScope(&ur) 58 | if !res || id <= 0 { 59 | t.Fail() 60 | } else { 61 | idSo = id 62 | } 63 | } 64 | 65 | func TestMySQLOauthDB_GetClientScopeList(t *testing.T) { 66 | res := odbSo.GetClientScopeList(cidSo) 67 | fmt.Println("scope list res: ", res) 68 | if res == nil || (*res)[0].ClientID != cidSo { 69 | t.Fail() 70 | } 71 | } 72 | 73 | func TestMySQLOauthDB_DeleteClientScope(t *testing.T) { 74 | res := odbSo.DeleteClientScope(idSo) 75 | fmt.Println("scope delete: ", res) 76 | if !res { 77 | t.Fail() 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /oauth2database/model.go: -------------------------------------------------------------------------------- 1 | package oauth2database 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | /* 8 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 9 | All rights reserved. 10 | 11 | Copyright (C) 2019 Ken Williamson 12 | All rights reserved. 13 | 14 | This program is free software: you can redistribute it and/or modify 15 | it under the terms of the GNU General Public License as published by 16 | the Free Software Foundation, either version 3 of the License, or 17 | (at your option) any later version. 18 | This program is distributed in the hope that it will be useful, 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | GNU General Public License for more details. 22 | You should have received a copy of the GNU General Public License 23 | along with this program. If not, see . 24 | 25 | */ 26 | 27 | //Client Client 28 | type Client struct { 29 | ClientID int64 `json:"clientId"` 30 | Secret string `json:"secret"` 31 | Name string `json:"name"` 32 | WebSite string `json:"webSite"` 33 | Email string `json:"email"` 34 | Enabled bool `json:"enabled"` 35 | Paid bool `json:"paid"` 36 | } 37 | 38 | //ClientRedirectURI ClientRedirectURI 39 | type ClientRedirectURI struct { 40 | ID int64 41 | URI string 42 | ClientID int64 43 | } 44 | 45 | //ClientAllowedURI ClientAllowedURI 46 | type ClientAllowedURI struct { 47 | ID int64 48 | URI string 49 | ClientID int64 50 | //Super bool 51 | } 52 | 53 | //ClientRole ClientRole 54 | type ClientRole struct { 55 | ID int64 56 | Role string 57 | ClientID int64 58 | //Super bool 59 | } 60 | 61 | //ClientScope ClientScope 62 | type ClientScope struct { 63 | ID int64 64 | Scope string 65 | ClientID int64 66 | } 67 | 68 | //ClientRoleURI ClientRoleURI 69 | type ClientRoleURI struct { 70 | ClientRoleID int64 71 | ClientAllowedURIID int64 72 | } 73 | 74 | //RoleURI RoleURI 75 | type RoleURI struct { 76 | ClientRoleID int64 77 | Role string 78 | ClientAllowedURIID int64 79 | ClientAllowedURI string 80 | ClientID int64 81 | } 82 | 83 | //RefreshToken RefreshToken 84 | type RefreshToken struct { 85 | ID int64 86 | Token string 87 | } 88 | 89 | //AccessToken AccessToken 90 | type AccessToken struct { 91 | ID int64 92 | Token string 93 | Expires time.Time 94 | RefreshTokenID int64 95 | } 96 | 97 | //start on AuthCode here 98 | 99 | //AuthorizationCode AuthorizationCode 100 | type AuthorizationCode struct { 101 | AuthorizationCode int64 102 | ClientID int64 103 | UserID string 104 | Expires time.Time 105 | AccessTokenID int64 106 | RandonAuthCode string 107 | AlreadyUsed bool 108 | Scope string 109 | } 110 | 111 | //AuthCodeScope AuthCodeScope 112 | type AuthCodeScope struct { 113 | ID int64 114 | Scope string 115 | AuthorizationCode int64 116 | } 117 | 118 | //AuthCodeRevolk AuthCodeRevolk 119 | type AuthCodeRevolk struct { 120 | ID int64 121 | AuthorizationCode int64 122 | } 123 | 124 | //ClientGrantType ClientGrantType 125 | type ClientGrantType struct { 126 | ID int64 127 | GrantType string 128 | ClientID int64 129 | } 130 | 131 | //ImplicitGrant ImplicitGrant 132 | type ImplicitGrant struct { 133 | ID int64 134 | ClientID int64 135 | UserID string 136 | AccessTokenID int64 137 | Scope string 138 | } 139 | 140 | //here add implicit grant scope 141 | 142 | //ImplicitScope ImplicitScope 143 | type ImplicitScope struct { 144 | ID int64 145 | Scope string 146 | ImplicitGrantID int64 147 | } 148 | 149 | //PasswordGrant PasswordGrant 150 | type PasswordGrant struct { 151 | ID int64 152 | ClientID int64 153 | UserID string 154 | AccessTokenID int64 155 | } 156 | 157 | //CredentialsGrant CredentialsGrant 158 | type CredentialsGrant struct { 159 | ID int64 160 | ClientID int64 161 | AccessTokenID int64 162 | } 163 | -------------------------------------------------------------------------------- /oauthclient/client.go: -------------------------------------------------------------------------------- 1 | package oauthclient 2 | 3 | import ( 4 | "net/http" 5 | ) 6 | 7 | /* 8 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 9 | All rights reserved. 10 | 11 | Copyright (C) 2019 Ken Williamson 12 | All rights reserved. 13 | 14 | This program is free software: you can redistribute it and/or modify 15 | it under the terms of the GNU General Public License as published by 16 | the Free Software Foundation, either version 3 of the License, or 17 | (at your option) any later version. 18 | This program is distributed in the hope that it will be useful, 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | GNU General Public License for more details. 22 | You should have received a copy of the GNU General Public License 23 | along with this program. If not, see . 24 | 25 | */ 26 | 27 | //Claim Claim 28 | type Claim struct { 29 | Role string 30 | URL string 31 | Scope string 32 | } 33 | 34 | //Client Client 35 | type Client interface { 36 | Authorize(r *http.Request, c *Claim) bool 37 | } 38 | -------------------------------------------------------------------------------- /oauthclient/coverage.out: -------------------------------------------------------------------------------- 1 | mode: set 2 | github.com/Ulbora/GoAuth2/oauthclient/mockOauthClient.go:37.69,63.2 1 1 3 | github.com/Ulbora/GoAuth2/oauthclient/mockOauthClient.go:66.49,70.2 3 1 4 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:43.65,51.25 8 1 5 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:51.25,53.3 1 1 6 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:55.2,59.23 5 1 7 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:59.23,62.27 2 1 8 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:62.27,64.25 2 1 9 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:64.25,66.5 1 1 10 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:66.10,68.5 1 1 11 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:69.4,79.31 11 1 12 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:82.2,82.12 1 1 13 | github.com/Ulbora/GoAuth2/oauthclient/oauthClient.go:86.45,90.2 3 1 14 | -------------------------------------------------------------------------------- /oauthclient/mockOauthClient.go: -------------------------------------------------------------------------------- 1 | //Package oauthclient ... 2 | package oauthclient 3 | 4 | import ( 5 | "net/http" 6 | 7 | m "github.com/Ulbora/GoAuth2/managers" 8 | ) 9 | 10 | /* 11 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 12 | All rights reserved. 13 | 14 | Copyright (C) 2019 Ken Williamson 15 | All rights reserved. 16 | 17 | This program is free software: you can redistribute it and/or modify 18 | it under the terms of the GNU General Public License as published by 19 | the Free Software Foundation, either version 3 of the License, or 20 | (at your option) any later version. 21 | This program is distributed in the hope that it will be useful, 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | GNU General Public License for more details. 25 | You should have received a copy of the GNU General Public License 26 | along with this program. If not, see . 27 | 28 | */ 29 | 30 | //MockOauthClient MockOauthClient 31 | type MockOauthClient struct { 32 | Manager m.Manager 33 | MockValid bool 34 | } 35 | 36 | //Authorize Authorize 37 | func (o *MockOauthClient) Authorize(r *http.Request, c *Claim) bool { 38 | // var mrtn bool 39 | // var mhashed bool 40 | // mtokenHeader := r.Header.Get("Authorization") 41 | // mclientIDStr := r.Header.Get("clientId") 42 | // mclientID, _ := strconv.ParseInt(mclientIDStr, 10, 64) 43 | // muserID := r.Header.Get("userId") 44 | // mhashedStr := r.Header.Get("hashed") 45 | // if mhashedStr == "true" { 46 | // mhashed = true 47 | // } 48 | // fmt.Println("tokenHeader", mtokenHeader) 49 | // fmt.Println("clientIDStr", mclientIDStr) 50 | // fmt.Println("clientID", mclientID) 51 | // fmt.Println("userID", muserID) 52 | // fmt.Println("hashed", mhashed) 53 | // if mtokenHeader != "" { 54 | // mtokenArray := strings.Split(mtokenHeader, " ") 55 | // fmt.Println("tokenArray", mtokenArray) 56 | // if len(mtokenArray) == 2 { 57 | // fmt.Println("tokenArray[1]", mtokenArray[1]) 58 | // mrtn = o.MockValid 59 | // } 60 | // } 61 | 62 | return o.MockValid 63 | } 64 | 65 | //GetNewClient GetNewClient 66 | func (o *MockOauthClient) GetNewClient() Client { 67 | var c Client 68 | c = o 69 | return c 70 | } 71 | -------------------------------------------------------------------------------- /oauthclient/mockOauthClient_test.go: -------------------------------------------------------------------------------- 1 | package oauthclient 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "testing" 7 | //m "github.com/Ulbora/GoAuth2/managers" 8 | ) 9 | 10 | func TestMockOauthClient_Authorize(t *testing.T) { 11 | 12 | var oc MockOauthClient 13 | oc.MockValid = true 14 | c := oc.GetNewClient() 15 | var cl Claim 16 | cl.Role = "testRole" 17 | cl.URL = "testURL" 18 | cl.Scope = "web" 19 | r, _ := http.NewRequest("GET", "/testurl", nil) 20 | r.Header.Set("Authorization", "Bearer jdljdfldjslkjdslkldksldfks") 21 | r.Header.Set("hashed", "true") 22 | r.Header.Set("clientId", "22") 23 | r.Header.Set("userId", "lfo") 24 | 25 | suc := c.Authorize(r, &cl) 26 | fmt.Println("suc", suc) 27 | if !suc { 28 | t.Fail() 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /oauthclient/oauthClient.go: -------------------------------------------------------------------------------- 1 | //Package oauthclient ... 2 | package oauthclient 3 | 4 | import ( 5 | "net/http" 6 | "strconv" 7 | "strings" 8 | 9 | cp "github.com/Ulbora/GoAuth2/compresstoken" 10 | m "github.com/Ulbora/GoAuth2/managers" 11 | lg "github.com/Ulbora/Level_Logger" 12 | ) 13 | 14 | /* 15 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 16 | All rights reserved. 17 | 18 | Copyright (C) 2019 Ken Williamson 19 | All rights reserved. 20 | 21 | This program is free software: you can redistribute it and/or modify 22 | it under the terms of the GNU General Public License as published by 23 | the Free Software Foundation, either version 3 of the License, or 24 | (at your option) any later version. 25 | This program is distributed in the hope that it will be useful, 26 | but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | GNU General Public License for more details. 29 | You should have received a copy of the GNU General Public License 30 | along with this program. If not, see . 31 | 32 | */ 33 | 34 | //OauthClient OauthClient 35 | type OauthClient struct { 36 | Manager m.Manager 37 | TokenCompressed bool 38 | JwtCompress cp.JwtCompress 39 | Log *lg.Logger 40 | } 41 | 42 | //Authorize Authorize 43 | func (o *OauthClient) Authorize(r *http.Request, c *Claim) bool { 44 | var rtn bool 45 | var hashed bool 46 | tokenHeader := r.Header.Get("Authorization") 47 | clientIDStr := r.Header.Get("clientId") 48 | clientID, _ := strconv.ParseInt(clientIDStr, 10, 64) 49 | userID := r.Header.Get("userId") 50 | hashedStr := r.Header.Get("hashed") 51 | if hashedStr == "true" { 52 | hashed = true 53 | } 54 | //fmt.Println("tokenHeader", tokenHeader) 55 | o.Log.Debug("clientIDStr", clientIDStr) 56 | o.Log.Debug("clientID", clientID) 57 | o.Log.Debug("userID", userID) 58 | o.Log.Debug("hashed", hashed) 59 | if tokenHeader != "" { 60 | tokenArray := strings.Split(tokenHeader, " ") 61 | //fmt.Println("tokenArray", tokenArray) 62 | if len(tokenArray) == 2 { 63 | var token string 64 | if o.TokenCompressed { 65 | token = o.JwtCompress.UnCompressJwt(tokenArray[1]) 66 | } else { 67 | token = tokenArray[1] 68 | } 69 | o.Log.Info("token:", token) 70 | var vr m.ValidateAccessTokenReq 71 | vr.AccessToken = token 72 | vr.Hashed = hashed 73 | vr.UserID = userID 74 | vr.ClientID = clientID 75 | vr.Role = c.Role 76 | vr.URI = c.URL 77 | vr.Scope = c.Scope 78 | rtn = o.Manager.ValidateAccessToken(&vr) 79 | o.Log.Debug("valid: ", rtn) 80 | } 81 | } 82 | return rtn 83 | } 84 | 85 | //GetNewClient GetNewClient 86 | func (o *OauthClient) GetNewClient() Client { 87 | var c Client 88 | c = o 89 | return c 90 | } 91 | -------------------------------------------------------------------------------- /oauthclient/oauthClient_test.go: -------------------------------------------------------------------------------- 1 | package oauthclient 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "testing" 7 | 8 | cp "github.com/Ulbora/GoAuth2/compresstoken" 9 | m "github.com/Ulbora/GoAuth2/managers" 10 | lg "github.com/Ulbora/Level_Logger" 11 | ) 12 | 13 | func TestOauthClient_Authorize(t *testing.T) { 14 | var man m.MockManager 15 | man.MockValidateAccessTokenSuccess = true 16 | 17 | var oc OauthClient 18 | oc.Manager = &man 19 | var l lg.Logger 20 | oc.Log = &l 21 | //oc.TokenCompressed = true 22 | c := oc.GetNewClient() 23 | var cl Claim 24 | cl.Role = "testRole" 25 | cl.URL = "testURL" 26 | cl.Scope = "web" 27 | r, _ := http.NewRequest("GET", "/testurl", nil) 28 | r.Header.Set("Authorization", "Bearer jdljdfldjslkjdslkldksldfks") 29 | r.Header.Set("hashed", "true") 30 | r.Header.Set("clientId", "22") 31 | r.Header.Set("userId", "lfo") 32 | 33 | suc := c.Authorize(r, &cl) 34 | fmt.Println("suc", suc) 35 | if !suc { 36 | t.Fail() 37 | } 38 | } 39 | 40 | func TestOauthClient_AuthorizeCompressed(t *testing.T) { 41 | var man m.MockManager 42 | man.MockValidateAccessTokenSuccess = true 43 | 44 | var oc OauthClient 45 | oc.Manager = &man 46 | oc.TokenCompressed = true 47 | var l lg.Logger 48 | oc.Log = &l 49 | c := oc.GetNewClient() 50 | var cl Claim 51 | cl.Role = "testRole" 52 | cl.URL = "testURL" 53 | cl.Scope = "web" 54 | var token = "jdljdfldjslkjdsdfgdfgdffgdfgfdfgdfgdfgdfgdfdfdfdfdfdfdfdfgdgdfgdffgdfgdfdfgfdfgdfdfgddddgdgdgdgdgdgdgddggdgdgdgdggdfgdfgdfgdgflkldksldfks" 55 | fmt.Println("len of token: ", len(token)) 56 | var jc cp.JwtCompress 57 | tkn := jc.CompressJwt(token) 58 | fmt.Println("tkn", tkn) 59 | fmt.Println("len of compressed token: ", len(tkn)) 60 | r, _ := http.NewRequest("GET", "/testurl", nil) 61 | r.Header.Set("Authorization", "Bearer "+tkn) 62 | r.Header.Set("hashed", "true") 63 | r.Header.Set("clientId", "22") 64 | r.Header.Set("userId", "lfo") 65 | 66 | suc := c.Authorize(r, &cl) 67 | fmt.Println("suc", suc) 68 | if !suc { 69 | t.Fail() 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /rolecontrol/coverage.out: -------------------------------------------------------------------------------- 1 | mode: set 2 | github.com/Ulbora/GoAuth2/rolecontrol/mockRoleControl.go:31.72,33.2 1 1 3 | github.com/Ulbora/GoAuth2/rolecontrol/mockRoleControl.go:36.84,38.2 1 1 4 | github.com/Ulbora/GoAuth2/rolecontrol/mockRoleControl.go:41.61,45.2 3 1 5 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:50.68,57.23 3 1 6 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:57.23,60.3 2 1 7 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:61.2,61.18 1 1 8 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:61.18,63.3 1 1 9 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:64.2,67.12 3 1 10 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:71.80,75.16 4 1 11 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:75.16,77.26 2 1 12 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:77.26,82.31 1 1 13 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:82.31,85.10 3 1 14 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:89.2,89.17 1 1 15 | github.com/Ulbora/GoAuth2/rolecontrol/roleControl.go:93.57,97.2 3 1 16 | -------------------------------------------------------------------------------- /rolecontrol/mockRoleControl.go: -------------------------------------------------------------------------------- 1 | //Package rolecontrol ... 2 | package rolecontrol 3 | 4 | /* 5 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 6 | All rights reserved. 7 | 8 | Copyright (C) 2019 Ken Williamson 9 | All rights reserved. 10 | 11 | This program is free software: you can redistribute it and/or modify 12 | it under the terms of the GNU General Public License as published by 13 | the Free Software Foundation, either version 3 of the License, or 14 | (at your option) any later version. 15 | This program is distributed in the hope that it will be useful, 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | GNU General Public License for more details. 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | 22 | */ 23 | 24 | //MockOauthAssets MockOauthAssets 25 | type MockOauthAssets struct { 26 | MockSuccess bool 27 | MockAllowedRole string 28 | } 29 | 30 | //AddControledURLs AddControledURLs 31 | func (c *MockOauthAssets) AddControledURLs(urls *[]ControlledURL) bool { 32 | return true 33 | } 34 | 35 | //GetControlledAsset GetControlledAsset 36 | func (c *MockOauthAssets) GetControlledAsset(url string, ca string) (bool, string) { 37 | return c.MockSuccess, c.MockAllowedRole 38 | } 39 | 40 | //GetNewAssetControl GetNewAssetControl 41 | func (c *MockOauthAssets) GetNewAssetControl() AssetControl { 42 | var ac AssetControl 43 | ac = c 44 | return ac 45 | } 46 | -------------------------------------------------------------------------------- /rolecontrol/mockRoleControl_test.go: -------------------------------------------------------------------------------- 1 | //Package rolecontrol ... 2 | package rolecontrol 3 | 4 | import ( 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | func TestMockOauthAssets_AddControledURLs(t *testing.T) { 10 | var cass ControlledAsset 11 | cass.ControlledAsset = "test" 12 | cass.AllowedRole = "admin" 13 | 14 | var cass2 ControlledAsset 15 | cass2.ControlledAsset = "test2" 16 | cass2.AllowedRole = "superAdmin" 17 | 18 | var caList = []ControlledAsset{cass, cass2} 19 | var cu ControlledURL 20 | cu.URL = "/test" 21 | cu.Asset = caList 22 | 23 | var cus = []ControlledURL{cu} 24 | 25 | var oa MockOauthAssets 26 | 27 | ac := oa.GetNewAssetControl() 28 | suc := ac.AddControledURLs(&cus) 29 | if !suc { 30 | t.Fail() 31 | } 32 | } 33 | 34 | func TestMockOauthAssets_GetControlledAsset(t *testing.T) { 35 | var oa MockOauthAssets 36 | oa.MockSuccess = true 37 | oa.MockAllowedRole = "admin" 38 | ac := oa.GetNewAssetControl() 39 | suc, role := ac.GetControlledAsset("/test", "test") 40 | fmt.Println("role: ", role) 41 | if !suc || role != "admin" { 42 | t.Fail() 43 | } 44 | } 45 | 46 | func TestMockOauthAssets_GetControlledAsset2(t *testing.T) { 47 | var oa MockOauthAssets 48 | oa.MockSuccess = true 49 | oa.MockAllowedRole = "superAdmin" 50 | ac := oa.GetNewAssetControl() 51 | suc, role := ac.GetControlledAsset("/test", "test2") 52 | fmt.Println("role: ", role) 53 | if !suc || role != "superAdmin" { 54 | t.Fail() 55 | } 56 | } 57 | 58 | func TestMockOauthAssets_GetControlledAsset3(t *testing.T) { 59 | var oa MockOauthAssets 60 | oa.MockSuccess = false 61 | //oa.MockAllowedRole = "superAdmin" 62 | ac := oa.GetNewAssetControl() 63 | suc, role := ac.GetControlledAsset("/test1", "test2") 64 | fmt.Println("role: ", role) 65 | if suc { 66 | t.Fail() 67 | } 68 | } 69 | 70 | func TestMockOauthAssets_GetControlledAsset4(t *testing.T) { 71 | var oa MockOauthAssets 72 | oa.MockSuccess = false 73 | //oa.MockAllowedRole = "superAdmin" 74 | ac := oa.GetNewAssetControl() 75 | suc, role := ac.GetControlledAsset("/test", "test22") 76 | fmt.Println("role: ", role) 77 | if suc { 78 | t.Fail() 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /rolecontrol/roleControl.go: -------------------------------------------------------------------------------- 1 | //Package rolecontrol ... 2 | package rolecontrol 3 | 4 | import "fmt" 5 | 6 | /* 7 | Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com) 8 | All rights reserved. 9 | 10 | Copyright (C) 2019 Ken Williamson 11 | All rights reserved. 12 | 13 | This program is free software: you can redistribute it and/or modify 14 | it under the terms of the GNU General Public License as published by 15 | the Free Software Foundation, either version 3 of the License, or 16 | (at your option) any later version. 17 | This program is distributed in the hope that it will be useful, 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | GNU General Public License for more details. 21 | You should have received a copy of the GNU General Public License 22 | along with this program. If not, see . 23 | 24 | */ 25 | 26 | //ControlledAsset ControlledAsset 27 | type ControlledAsset struct { 28 | ControlledAsset string `json:"controlledAsset"` 29 | AllowedRole string `json:"allowedRole"` 30 | } 31 | 32 | //ControlledURL ControlledURL 33 | type ControlledURL struct { 34 | URL string `json:"url"` 35 | Asset []ControlledAsset `json:"assets"` 36 | } 37 | 38 | //AssetControl AssetControl 39 | type AssetControl interface { 40 | AddControledURLs(urls *[]ControlledURL) bool 41 | GetControlledAsset(url string, ca string) (bool, string) 42 | } 43 | 44 | //OauthAssets OauthAssets 45 | type OauthAssets struct { 46 | m map[string]*[]ControlledAsset 47 | } 48 | 49 | //AddControledURLs AddControledURLs 50 | func (c *OauthAssets) AddControledURLs(urls *[]ControlledURL) bool { 51 | var rtn bool 52 | //creates a map by url of prohibited sections to be used in a particular url 53 | c.m = make(map[string]*[]ControlledAsset) 54 | //for example 55 | //url https://addTest/admin/user 56 | //could require role superUser 57 | for i := range *urls { 58 | var u = (*urls)[i] 59 | c.m[u.URL] = &u.Asset 60 | } 61 | if len(c.m) > 0 { 62 | rtn = true 63 | } 64 | fmt.Println("assess: ", c.m) 65 | fmt.Println("assess list: ", c.m["/ulbora/rs/clientAllowedUri/add"]) 66 | 67 | return rtn 68 | } 69 | 70 | //GetControlledAsset GetControlledAsset 71 | func (c *OauthAssets) GetControlledAsset(url string, ca string) (bool, string) { 72 | var suc bool 73 | var rtn string 74 | cas := c.m[url] 75 | if cas != nil { 76 | fmt.Println("cas: ", cas) 77 | for _, a := range *cas { 78 | //example 79 | //url https://addTest/admin/user 80 | //with controlled asset "admin" 81 | //could require role superUser 82 | if a.ControlledAsset == ca { 83 | suc = true 84 | rtn = a.AllowedRole 85 | break 86 | } 87 | } 88 | } 89 | return suc, rtn 90 | } 91 | 92 | //GetNewAssetControl GetNewAssetControl 93 | func (c *OauthAssets) GetNewAssetControl() AssetControl { 94 | var ac AssetControl 95 | ac = c 96 | return ac 97 | } 98 | -------------------------------------------------------------------------------- /rolecontrol/roleControl_test.go: -------------------------------------------------------------------------------- 1 | //Package rolecontrol ... 2 | package rolecontrol 3 | 4 | import ( 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | var ac AssetControl 10 | 11 | func TestOauthAssets_AddControledURLs(t *testing.T) { 12 | var cass ControlledAsset 13 | cass.ControlledAsset = "test" 14 | cass.AllowedRole = "admin" 15 | 16 | var cass2 ControlledAsset 17 | cass2.ControlledAsset = "test2" 18 | cass2.AllowedRole = "superAdmin" 19 | 20 | var caList = []ControlledAsset{cass, cass2} 21 | var cu ControlledURL 22 | cu.URL = "/test" 23 | cu.Asset = caList 24 | 25 | var cus = []ControlledURL{cu} 26 | 27 | var oa OauthAssets 28 | ac = oa.GetNewAssetControl() 29 | suc := ac.AddControledURLs(&cus) 30 | if !suc { 31 | t.Fail() 32 | } 33 | } 34 | 35 | func TestOauthAssets_GetControlledAsset(t *testing.T) { 36 | suc, role := ac.GetControlledAsset("/test", "test") 37 | fmt.Println("role: ", role) 38 | if !suc || role != "admin" { 39 | t.Fail() 40 | } 41 | } 42 | 43 | func TestOauthAssets_GetControlledAsset2(t *testing.T) { 44 | suc, role := ac.GetControlledAsset("/test", "test2") 45 | fmt.Println("role: ", role) 46 | if !suc || role != "superAdmin" { 47 | t.Fail() 48 | } 49 | } 50 | 51 | func TestOauthAssets_GetControlledAsset3(t *testing.T) { 52 | suc, role := ac.GetControlledAsset("/test1", "test2") 53 | fmt.Println("role: ", role) 54 | if suc { 55 | t.Fail() 56 | } 57 | } 58 | 59 | func TestOauthAssets_GetControlledAsset4(t *testing.T) { 60 | suc, role := ac.GetControlledAsset("/test", "test22") 61 | fmt.Println("role: ", role) 62 | if suc { 63 | t.Fail() 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /runtest.sh: -------------------------------------------------------------------------------- 1 | cd mysqldb 2 | go test -coverprofile=coverage.out 3 | sleep 15 4 | cd .. 5 | cd managers 6 | go test -coverprofile=coverage.out 7 | sleep 15 8 | cd .. 9 | cd oauthclient 10 | go test -coverprofile=coverage.out 11 | sleep 15 12 | cd .. 13 | cd rolecontrol 14 | go test -coverprofile=coverage.out 15 | sleep 15 16 | cd .. 17 | cd handlers 18 | go test -coverprofile=coverage.out 19 | sleep 15 20 | cd .. 21 | cd compresstoken 22 | go test -coverprofile=coverage.out 23 | sleep 15 24 | -------------------------------------------------------------------------------- /scripts/temp.sql: -------------------------------------------------------------------------------- 1 | SELECT * 2 | FROM client_role cr inner join 3 | uri_role ur on cr.id = ur.client_role_id 4 | left join client_allowed_uri cau on cau.id = ur.client_allowed_uri_id 5 | WHERE cr.client_id = 403 6 | order by ur.client_role_id 7 | 8 | SELECT cr.id as role_id, cr.role, cau.id as uri_id, cau.uri, cr.client_id 9 | FROM client_role cr inner join 10 | uri_role ur on cr.id = ur.client_role_id 11 | left join client_allowed_uri cau on cau.id = ur.client_allowed_uri_id 12 | WHERE cr.client_id = 403 13 | order by ur.client_role_id 14 | 15 | 16 | 17 | SELECT a.authorization_code, a.client_id, s.scope 18 | FROM authorization_code a inner join auth_code_scope s 19 | on a.authorization_code = s.authorization_code 20 | WHERE a.client_id = ? and a.user_id = ? and s.scope = ? -------------------------------------------------------------------------------- /scripts/ulboraOauth2.mwb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ulbora/GoAuth2/5f98770fcff7df6f14cc176de2f11bf0ac60979b/scripts/ulboraOauth2.mwb -------------------------------------------------------------------------------- /scripts/ulboraOauth2.mwb.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ulbora/GoAuth2/5f98770fcff7df6f14cc176de2f11bf0ac60979b/scripts/ulboraOauth2.mwb.bak -------------------------------------------------------------------------------- /server_test.go1: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | //"os/exec" 6 | "testing" 7 | ) 8 | 9 | func Test_main(t *testing.T) { 10 | os.Setenv("PORT", "4000") 11 | main() 12 | } 13 | -------------------------------------------------------------------------------- /sonar-project.properties: -------------------------------------------------------------------------------- 1 | # must be unique in a given SonarQube instance 2 | sonar.projectKey=Ulbora_GoAuth2 3 | # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. 4 | sonar.projectName=GoAuth2 5 | sonar.projectVersion=1.0 6 | sonar.organization=ulboralabs 7 | 8 | # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. 9 | # This property is optional if sonar.modules is set. 10 | sonar.sources=. 11 | sonar.exclusions=**/*_test.go,**/vendor/**,scripts/**/*,nbproject/**/*,mysqldb/queries.go,server.go,managers/manager.go,handlers/handlers.go,oauth2database/**/*,static/**/* 12 | sonar.test.inclusions=**/*_test.go 13 | sonar.host.url=https://sonarcloud.io 14 | sonar.go.coverage.reportPaths=mysqldb/coverage.out, managers/coverage.out, oauthclient/coverage.out, rolecontrol/coverage.out, handlers/coverage.out, compresstoken/coverage.out 15 | 16 | 17 | # Encoding of the source code. Default is default system encoding 18 | #sonar.sourceEncoding=UTF-8 -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./main -compressJwt -assets=WwogICB7CiAgICAgICJ1cmwiOiIvdWxib3JhL3JzL2NsaWVudEFsbG93ZWRVcmkvYWRkIiwKICAgICAgImFzc2V0cyI6WwogICAgICAgICB7CiAgICAgICAgICAgICJjb250cm9sbGVkQXNzZXQiOiJ1bGJvcmEiLAogICAgICAgICAgICAiYWxsb3dlZFJvbGUiOiJzdXBlckFkbWluIgogICAgICAgICB9CiAgICAgIF0KICAgfSwKICAgewogICAgICAidXJsIjoiL3VsYm9yYS9ycy9jbGllbnRBbGxvd2VkVXJpL3VwZGF0ZSIsCiAgICAgICJhc3NldHMiOlsKICAgICAgICAgewogICAgICAgICAgICAiY29udHJvbGxlZEFzc2V0IjoidWxib3JhIiwKICAgICAgICAgICAgImFsbG93ZWRSb2xlIjoic3VwZXJBZG1pbiIKICAgICAgICAgfQogICAgICBdCiAgIH0sCiAgIHsKICAgICAgInVybCI6Ii91bGJvcmEvcnMvY2xpZW50Um9sZS9hZGQiLAogICAgICAiYXNzZXRzIjpbCiAgICAgICAgIHsKICAgICAgICAgICAgImNvbnRyb2xsZWRBc3NldCI6InN1cGVyQWRtaW4iLAogICAgICAgICAgICAiYWxsb3dlZFJvbGUiOiJzdXBlckFkbWluIgogICAgICAgICB9CiAgICAgIF0KICAgfQpd 4 | 5 | 6 | -------------------------------------------------------------------------------- /startMock.sh: -------------------------------------------------------------------------------- 1 | go run server.go -mock -------------------------------------------------------------------------------- /static/authorizeApp.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{template "head"}} 4 | 5 | 6 |
7 |
8 |

Authorize Application

9 |

{{.ClientName}} would like to access your account.

10 |
11 |
12 |
13 |

Permission Review

14 |
15 |
    16 |
  • {{.Scope}}
  • 17 |
18 |
19 |
20 |
21 | 23 |
24 |
25 | 27 |
28 |
29 | 30 |
31 |
32 |

{{.ClientName}}

33 |
{{.WebSite}}
34 |
35 |
36 |
37 | 38 | 39 | -------------------------------------------------------------------------------- /static/css/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: #286090; 3 | } 4 | .home-top { 5 | margin: 0 0 0 0; 6 | padding: 0 0 30% 0; 7 | width: 100%; 8 | background: linear-gradient(to bottom, cadetblue 0%, lightsteelblue 100%); 9 | } 10 | .oauth2-home-title { 11 | width: 100%; 12 | text-align: center; 13 | float: left; 14 | margin: 0 0 0 0; 15 | padding: 12% 0 0 0; 16 | color: blue; 17 | } 18 | .oauth2-home-text { 19 | width: 100%; 20 | text-align: center; 21 | float: left; 22 | padding: 5% 0 0 0; 23 | color: darkblue; 24 | } 25 | .oauth-login-form { 26 | margin: 20% 0 0 25%; 27 | padding: 2%; 28 | width: 50%; 29 | background: #85a3b1; 30 | border-radius: 25px; 31 | border: 2px solid rgba(127, 127, 167, 0.425); 32 | } 33 | .message { 34 | text-align: center; 35 | margin: 1% 0 0 0; 36 | } 37 | .authorize-app { 38 | border-top: 1px solid #286090; 39 | width: 100%; 40 | margin: 2% 0 0 0; 41 | float: left; 42 | } 43 | .authorize-app-left { 44 | width: 50%; 45 | float: left; 46 | } 47 | .authorize-app-right { 48 | width: 50%; 49 | float: left; 50 | border: 1px solid #286090; 51 | padding: 1% 0 1% 1%; 52 | margin: 2% 0 0 0; 53 | } 54 | .authorize-app-element { 55 | width: 100%; 56 | } 57 | .authorize-app-button-left { 58 | display: inline; 59 | padding: 3% 10% 0 0; 60 | text-align: right; 61 | } 62 | .authorize-app-button-right { 63 | display: inline; 64 | padding: 3% 10% 0 0; 65 | text-align: left; 66 | } 67 | -------------------------------------------------------------------------------- /static/css/styles.less: -------------------------------------------------------------------------------- 1 | body { 2 | color: #286090; 3 | } 4 | 5 | 6 | .home-top { 7 | margin: 0 0 0 0; 8 | padding: 0 0 30% 0; 9 | width: 100%; 10 | background: linear-gradient(to bottom, cadetblue 0%, lightsteelblue 100%); 11 | 12 | } 13 | 14 | .oauth2-home-title { 15 | width: 100%; 16 | text-align: center; 17 | float: left; 18 | margin: 0 0 0 0; 19 | padding: 12% 0 0 0; 20 | color: blue; 21 | // background: red; 22 | } 23 | 24 | .oauth2-home-text { 25 | width: 100%; 26 | text-align: center; 27 | float: left; 28 | padding: 5% 0 0 0; 29 | color: darkblue; 30 | } 31 | 32 | .oauth-login-form { 33 | margin: 20% 0 0 25%; 34 | padding: 2%; 35 | width: 50%; 36 | background: rgb(133, 163, 177); 37 | border-radius: 25px; 38 | border: 2px solid rgba(127, 127, 167, 0.425); 39 | } 40 | 41 | .message { 42 | text-align: center; 43 | margin: 1% 0 0 0; 44 | } 45 | 46 | .authorize-app { 47 | border-top: 1px solid #286090; 48 | width: 100%; 49 | margin: 2% 0 0 0; 50 | float: left; 51 | // background: red; 52 | } 53 | 54 | .authorize-app-left { 55 | //display: inline; 56 | // text-align: center; 57 | width: 50%; 58 | float: left; 59 | } 60 | 61 | .authorize-app-right { 62 | //display: inline; 63 | width: 50%; 64 | float: left; 65 | border: 1px solid #286090; 66 | padding: 1% 0 1% 1%; 67 | margin: 2% 0 0 0; 68 | } 69 | 70 | .authorize-app-element { 71 | width: 100%; 72 | } 73 | 74 | .authorize-app-button-left { 75 | display: inline; 76 | // width: 50%; 77 | // float: left; 78 | padding: 3% 10% 0 0; 79 | text-align: right; 80 | } 81 | 82 | .authorize-app-button-right { 83 | display: inline; 84 | // width: 50%; 85 | // float: right; 86 | padding: 3% 10% 0 0; 87 | text-align: left; 88 | } -------------------------------------------------------------------------------- /static/head.html: -------------------------------------------------------------------------------- 1 | {{define "head"}} 2 | 3 | 4 | GoAuth2 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | {{end}} -------------------------------------------------------------------------------- /static/images/goAuth2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ulbora/GoAuth2/5f98770fcff7df6f14cc176de2f11bf0ac60979b/static/images/goAuth2.png -------------------------------------------------------------------------------- /static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{template "head"}} 4 | 5 | 6 |
7 |

GoAuth2

8 |
9 | 10 |

Contact: www.ulboralabs.com for a client account on GoAuth2.

11 |

A complete standalone OAuth2 Server RFC 6749 implementation written in Golang and 12 | licensed under the GPL V3 license.

13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /static/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{template "head"}} 4 | 5 | 6 |
7 | 26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /static/oauthError.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{template "head"}} 4 | 5 | 6 |
7 |
8 |

Error

9 |
10 |
11 |

{{.Error}}

12 |
13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /updateMods.sh: -------------------------------------------------------------------------------- 1 | # GO111MODULE=on go get -u 2 | go get -u all ./... 3 | --------------------------------------------------------------------------------