Giter Club home page Giter Club logo

gradio_log's Introduction

用法

casdoor

casdoor是一个CAS认证,介绍链接🔗

Gradio

介绍链接🔗

用途

gradio有自己的登录界面与需求,但是只是简单认证

def same_auth(username, password):
    return username == password
demo.launch(auth=same_auth)

Oatuh认证是一个认证功能,只能在huagging face中使用。

import gradio as gr


def hello(profile: gr.OAuthProfile | None) -> str:
    if profile is None:
        return "I don't know you."
    return f"Hello {profile.name}"


with gr.Blocks() as demo:
    gr.LoginButton()
    gr.LogoutButton()
    gr.Markdown().attach_load_event(hello, None)

这里就有需要就是希望通过login username password进入界面,那就是需要通过Oauth进行第三方认证。

这里选择casdoor 启动服务使用docker进行启动,利用-d命令使其不停止一直挂在挂载。

代码

想要与casdoor交互,首先要设置这些变量,具体含义查看casdoor文档📄

endpoint = 'http://ip:8000'
client_id = ''
client_secret = ''
certificate = b'''-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----'''
org_name = 'built-in'
states = "test"
scope = "read"
redirect_uri = "http://ip:8000/callback"

之后就是要构造casdoorSDKurl

sdk = CasdoorSDK(
        endpoint,
        client_id,
        client_secret,
        certificate = certificate,
        org_name = org_name,
        application_name='test'
    )
auth_url = f"{endpoint}/login/oauth/authorize?client_id={client_id}&response_type=code" \
               f"&redirect_uri={redirect_uri}" \
               f"&scope=read&state={states}"

之后利用request去访问auth_url 然后将获取的内容进行解析

# 获取重定向后的 URL
    redirected_url = responses.url

    # 解析 URL 获取 code 和 state
    parsed_url = urlparse(redirected_url)
    query_params = parse_qs(parsed_url.query)
    code = query_params.get('code')
    state = query_params.get('state')
    print(state)
    print(code)

    # 使用 code 获取访问令牌
    # 使用 await 关键字来等待协程的执行
    token = sdk.get_oauth_token(code=code)
    print(token)
    # 提取访问令牌
    access_token = token.get("access_token")
    username_to_check = usesrname
    user = sdk.get_user(username_to_check)
    if user is not None:
        return True
    else:
        return False

具体过程参看文档📄

gradio_log's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.